我有以下代码行不断给我一个错误,即引擎对象没有对象执行。我认为我一切都对,但不知道接下来会发生什么。似乎其他人也遇到了这个问题,重新启动他们的笔记本电脑就可以了。我正在使用 Pycharm 并已重新启动但没有任何解决方案。任何帮助是极大的赞赏!
import pandas as pd
from sqlalchemy import create_engine, text
import sqlalchemy
import pymysql
masterlist = pd.read_excel('Masterlist.xlsx')
user = 'root'
pw = 'test!*'
db = 'hcftest'
engine = create_engine("mysql+pymysql://{user}:{pw}@localhost:3306/{db}"
.format(user=user, pw=pw, db=db))
results = engine.execute(text("SELECT * FROM companyname;"))
for row in results:
print(row)
从 1.4 到 2.0 发生了变化。我相信上面的代码在 sqlalchemy 1.4 版本上可以正常运行。环境SQLALCHEMY_WARN_20=1 python
运行上面的代码会显示此警告:
<stdin>:1: RemovedIn20Warning: The Engine.execute() method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by the Connection.execute() method of Connection, or in the ORM by the Session.execute() method of Session. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
所以现在正确的代码执行方式是:
with engine.connect() as conn:
result = conn.execute(stmt)
source 这里描述 1.4 中的行为 https://docs.sqlalchemy.org/en/14/core/connections.html#dbengine-implicit and 这里描述 2.0 中的行为 https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-20-implicit-execution
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)