使用 SQLAlchemy(版本 1.4.44)创建、删除或以其他方式修改表时,更新似乎未提交。为了解决这个问题,我遵循文档并使用 commit() 函数。这是一个简单的例子
from sqlalchemy import create_engine, text
engine = create_engine("postgresql://user:password@connection_string:5432/database_name")
with engine.connect() as connection:
sql = "create table test as (select count(1) as result from userquery);"
result = connection.execute(text(sql))
connection.commit()
Run Code Online (Sandbox Code Playgroud)
这会产生错误:
AttributeError: 'Connection' object has no attribute 'commit'
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
Ian*_*son 16
对这个问题的评论是正确的,您正在查看 2.0 文档,但您需要做的就是future=True在调用时create_engine()设置使用 2.0 中提供的“立即提交”功能。
\n\n当使用带有 create_engine.future 标志的 2.0 样式时,也可以使用 \xe2\x80\x9ccommit as\nyou go\xe2\x80\x9d 样式,因为连接具有自动开始\n行为,该行为在首次调用语句时发生在没有显式调用 Connection.begin() 的情况下:
\n
| 归档时间: |
|
| 查看次数: |
11718 次 |
| 最近记录: |