可以安装PyMySQL模块并使用它来连接MySQL数据库,而不是使用mysql.connector模块。
以下是使用PyMySQL模块连接MySQL数据库的示例代码:
import pymysql
#连接MySQL数据库
connection = pymysql.connect(host='localhost',
user='root',
password='password',
db='database_name',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
#执行SQL语句
sql = "SELECT * FROM `table_name`"
cursor.execute(sql)
#获取结果
result = cursor.fetchone()
print(result)
finally:
connection.close()
需要先安装PyMySQL模块:
pip install PyMySQL