使用sqlalchemy连接到MySQL时没有属性错误

2024-06-25 23:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用带有PyMySQL或mysqlconnector的SQLAlchemy引擎连接到本地MySQL数据库(两者都试过了)。我需要这个图书馆,而不是与熊猫工作的基本联系。在

对于这两个问题,我都收到了错误

"AttributeError: module 'sqlalchemy.sql.sqltypes' has no attribute 'NativeForEmulated'".

import mysql.connector
import pymysql
from sqlalchemy import create_engine

# Connection works
conn = mysql.connector.connect(host='localhost',
                                   database='db',
                                   user='user',
                                   password='pw')

# Connection throws error
engine = create_engine('mysql+pymysql://user:pw@127.0.0.1:3306/db')
engine = create_engine("mysql+mysqlconnector://user:pw@localhost/db")

我还没有找到任何解决方案,因为使用这些库进行连接似乎很简单。有什么想法吗?提前谢谢。在


Tags: importlocalhostdbconnectorsqlalchemycreatemysqlconnection