conn = pymssqlconnect(host=r"localhost",user=r"sa",password=r"pwd",database=r"production")
stack overflow 上有个类似的问题 有个答案是这样的 你试试 没用过mssql
由于版本的不同,Python 连接 Hive 的方式也就不一样。
在网上搜索关键字 python hive 的时候可以找到一些解决方案。大部分是这样的,首先把hive 根目录下的$HIVE_HOME/lib/py拷贝到 python 的库中,也就是 site-package 中,或者干脆把新写的 python 代码和拷贝的 py 库放在同一个目录下,然后用这个目录下提供的 thrift 接口调用。示例也是非常简单的。类似这样:
import sys
from hive_service import ThriftHive
from hive_servicettypes import HiveServerException
from thrift import Thrift
from thrifttransport import TSocket
from thrifttransport import TTransport
from thriftprotocol import TBinaryProtocol
def hiveExe(sql):
try:
transport = TSocketTSocket('127001', 10000)
transport = TTransportTBufferedTransport(transport)
protocol = TBinaryProtocolTBinaryProtocol(transport)
client = ThriftHiveClient(protocol)
transportopen()
clientexecute(sql)
print "The return value is : "
print clientfetchAll()
print ""
transportclose()
except ThriftTException, tx:
print '%s' % (txmessage)
if __name__ == '__main__':
hiveExe("show tables")1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728
或者是这样的:
#!/usr/bin/env python
import sys
from hive import ThriftHive
from hivettypes import HiveServerException
from thrift import Thrift
from thrifttransport import TSocket
from thrifttransport import TTransport
from thriftprotocol import TBinaryProtocol
try:
transport = TSocketTSocket('1418154188', 10000)
transport = TTransportTBufferedTransport(transport)
protocol = TBinaryProtocolTBinaryProtocol(transport)
client = ThriftHiveClient(protocol)
transportopen()
clientexecute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
clientexecute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
clientexecute("SELECT FROM test1")
while (1):
row = clientfetchOne()
if (row == None):
break
print rowve
clientexecute("SELECT FROM test1")
print clientfetchAll()
transportclose()
except ThriftTException, tx:
print '%s' % (txmessage)
12345678910111213141516171819202122232425262728293031323334351234567891011121314151617181920212223242526272829303132333435
但是都解决不了问题,从 netstat 中查看可以发现 TCP 连接确实是建立了,但是不执行 hive 指令。也许就是版本的问题。
还是那句话,看各种中文博客不如看官方文档。
项目中使用的 hive 版本是013,此时此刻官网的最新版本都到了121了。中间间隔了120、110、100、0140。但是还是参考一下官网的方法试试吧。
首先看官网的 setting up hiveserver2
可以看到启动 hiveserver2 可以配置最大最小线程数,绑定的 IP,绑定的端口,还可以设置认证方式。(之前一直不成功正式因为这个连接方式)然后还给了 python 示例代码。
import pyhs2
with pyhs2connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='root',
password='test',
database='default') as conn:
with conncursor() as cur:
#Show databases
print curgetDatabases()
#Execute query
curexecute("select from table")
#Return column info from query
print curgetSchema()
#Fetch table results
for i in curfetch():
print i123456789101112131415161718192021123456789101112131415161718192021
在拿到这个代码的时候,自以为是的把认证信息给去掉了。然后运行发现跟之前博客里介绍的方法结果一样,建立了 TCP 连接,但是就是不执行,也不报错。这是几个意思?然后无意中尝试了一下原封不动的使用上面的代码。结果可以用。唉。。。
首先声明一下,hive-sitexml中默认关于 hiveserver2的配置我一个都没有修改,一直是默认配置启动 hiveserver2。没想到的是默认配置是有认证机制的。
然后再写一点,在安装 pyhs2的时候还是遇到了点问题,其实还是要看官方文档的,我只是没看官方文档直接用 pip安装导致了这个问题。安装 pyhs2需要确定已经安装了几个依赖包。直接看在 github 上的 wiki 吧。哪个没安装就补上哪一个就好了。
MySQLdbconnect是python 连接MySQL数据库的方法,在Python中 import MySQLdb即可使用,至于connect中的参数很简单:\x0d\host:MySQL服务器名\x0d\user:数据库使用者\x0d\password:用户登录密码\x0d\db: *** 作的数据库名\x0d\charset:使用的字符集(一般是gb2312)\x0d\cursor = dbcursor() 其实就是用来获得python执行Mysql命令的方法,也就是\x0d\我们所说的 *** 作游标\x0d\下面cursorexecute则是真正执行MySQL语句,即查询TABLE_PARAMS表的数据。\x0d\至于fetchall()则是接收全部的返回结果行 row就是在python中定义的一个变量,用来接收返回结果行的每行数据。同样后面的r也是一个变量,用来接收row中的每个字符,如果写成C的形式就更好理解了\x0d\for(string row = ''; row回答于 2022-11-16
settingspy
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'
'NAME': '库名', # Or path to database file if using sqlite3
'USER': 'root', # Not used with sqlite3
'PASSWORD': '', # Not used with sqlite3
'HOST': '', # Set to empty string for localhost Not used with sqlite3
'PORT': '3306', # Set to empty string for default Not used with sqlite3
},
'库名': {
'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'
'NAME': '库名', # Or path to database file if using sqlite3
'USER': 'root', # Not used with sqlite3
'PASSWORD': '', # Not used with sqlite3
'HOST': '', # Set to empty string for localhost Not used with sqlite3
'PORT': '3306', # Set to empty string for default Not used with sqlite3
}
}
以上就是关于python连接数据库后进行 *** 作时出现错误全部的内容,包括:python连接数据库后进行 *** 作时出现错误、windows下怎么用python连接hive数据库、python连接MySQL数据库问题 cursor( ) 、execute()和fetc等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)