我使用Anaconda和python3.7,并试图使用以下代码连接到远程数据库:
import MySQLdb
myDB = MySQLdb.connect(host="xxxxx", port=3306, user="xxx",password="xxxx",db="xxxx")但我得到了以下错误:
File "C:\Users\zanto\anaconda3\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2006, 'SSL connection error: unknown error number')我在mysql中尝试了两个用户,一个使用%,另一个使用localhost,但是仍然会出现相同的错误。
发布于 2020-04-05 11:06:48
这是mySQLdb库的一个问题。
我通过pymysql安装了anaconda库,现在它开始工作了!
我的新代码是:
import pymysql.cursors
connection = pymysql.connect(host='xxxx',port=3306,user="xxxx",password="xxxx",db="xxxx", cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql="SELECT * FROM Table WHERE Field = 'value'"
cursor.execute(sql)
results = cursor.fetchall()
#print (results) #if you remove the comment you will get the query result as a dictionary
for record in results:
record_line = " ".join('{0}{1}'.format(field,value) for field,value in record.items())
print(record_line)
finally:
connection.close()更多信息:游标允许MySQL代码在数据库会话中执行MySQL命令。游标是由connection.cursor()方法创建的:它们绑定到连接的整个生命周期,所有命令都在连接包装的数据库会话上下文中执行。
cursor.execute()方法在MySQL数据库中运行查询。cursor.fetchall()方法以列表形式返回查询结果,其中包含作为字典的记录。
https://stackoverflow.com/questions/61033635
复制相似问题