MySQL使用连接器/Python



我正试图用python连接到远程服务器上的数据库,我正在使用以下指南

有两种方法被提议

import mysql.connector
cnx = mysql.connector.connect(user='scott', password='password',
host='127.0.0.1',
database='employees')
print(cnx) # <mysql.connector.connection_cext.CMySQLConnection object at 0x0000023EDDCD9FA0>
print(cnx.is_connected()) # Gives FALSE
cursor = cnx.cursor() # Gives mysql.connector.errors.OperationalError: MySQL Connection not available.
import mysql.connector
cnx = mysql.connector.MySQLConnection(user='scott', password='password',
host='127.0.0.1',
database='employees')
print(cnx) # Gives <mysql.connector.connection.MySQLConnection object at 0x00000158D5C99FD0>
print(cnx.is_connected) # Gives TRUE
cursor = cnx .cursor() # No ERROR

我在使用第一种方法时出错。

我可以通过使用:use_pure = True来修复它如果您看到connect()的实现

if HAVE_CEXT and not use_pure:
return CMySQLConnection(*args, **kwargs)
return MySQLConnection(*args, **kwargs)

最新更新