简体   繁体   English

MySQL 的 sqlalchemy+pyodbc 在尝试从引擎创建连接时给我一个错误

[英]sqlalchemy+pyodbc for MySQL is giving me an error while trying to create a connection from engine

I am trying to connect to a MySQL db using sqlalchemy and pyodbc.我正在尝试使用 sqlalchemy 和 pyodbc 连接到 MySQL 数据库。 Connecting using ODBC is a requirement, so I cannot use any other methods for creating engine and connection.使用 ODBC 连接是一项要求,因此我不能使用任何其他方法来创建引擎和连接。 Here the code I am using to create engine and connection.这是我用来创建引擎和连接的代码。

params = quote_plus("DRIVER={MySQL ODBC 8.0 Unicode Driver};"
                                f"SERVER={host}:{port};"
                                f"DATABASE={db};"
                                f"UID={username};"
                                f"PWD={password}")

# Creating dbengine and connection
db_engine = create_engine(f"mysql+pyodbc:///?odbc_connect={params}")
print('Type of dbengine', type(db_engine))
connection = db_engine.connect()

I am giving the last 3 lines of the stack trace I am getting from connection = db_engine.connect() .我给出了从connection = db_engine.connect()获得的堆栈跟踪的最后 3 行。

  File "C:\ProgramData\Anaconda3\envs\nice_rpa\lib\site-packages\sqlalchemy\engine\base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "C:\ProgramData\Anaconda3\envs\nice_rpa\lib\site-packages\sqlalchemy\engine\default.py", line 608, in do_execute
    cursor.execute(statement, parameters)
TypeError: The first argument to execute must be a string or unicode query.

There is no query I am passing or as per the connect() doc, connect takes only 1 kwarg and no query.我没有传递任何查询,或者根据 connect() 文档,connect 只需要 1 kwarg 并且没有查询。 I don't understand why I am getting this error.我不明白为什么会收到此错误。 Can someone explain what and why this error?有人可以解释这个错误的原因和原因吗? Also, seems like pyodbc gives a lot of errors sometimes(as per other resources I read).此外,似乎 pyodbc 有时会给出很多错误(根据我阅读的其他资源)。 Is there a good alternative for sqlalchemy+pyodbc. sqlalchemy+pyodbc 有没有好的替代方案? I finally want to pass the connection to pd.to_sql and `pd.read_sql' and using odbc is mandatory.我最终想将connection传递给pd.to_sql和 `pd.read_sql',并且必须使用 odbc。

This was an issue with SQLAlchemy 1.3.x and mysql+pyodbc:// that has been fixed in SQLAlchemy 1.4.这是 SQLAlchemy 1.3.x 和mysql+pyodbc://的问题,已在 SQLAlchemy 1.4 中修复。

This issue was persistent with SQLAlchemy >1.4.4 I have uninstall Sqlalchemy and install 1.4.0 version which solved the issue这个问题一直存在于 SQLAlchemy >1.4.4 我已经卸载 Sqlalchemy 并安装 1.4.0 版本解决了这个问题

pip uninstall SQLAlchemy
pip install SQLAlchemy==1.4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM