gpt4 book ai didi

python - 使用 connection.execute() 的 sqlalchemy 原始 sql 查询限制

转载 作者:太空狗 更新时间:2023-10-29 21:44:07 26 4
gpt4 key购买 nike

这段 python 代码应该在数据库上运行语句,但是没有执行 sql 语句:

from sqlalchemy import *
sql_file = open("test.sql","r")
sql_query = sql_file.read()
sql_file.close()
engine = create_engine(
'postgresql+psycopg2://user:password@localhost/test', echo=False)

conn = engine.connect()
print sql_query
result = conn.execute(sql_query)
conn.close()

test.sql 文件包含创建 89 个表的 SQL 语句。

如果我指定 89 个表,则不会创建表,但如果我将表数减少到 2 个,它就可以工作。

在 conn.execute 中可以执行的查询数量是否有限制?如何运行任意数量的原始查询?

最佳答案

也许,强制自动提交:

conn.execute(RAW_SQL).execution_options(autocommit=True))

其他方法是使用事务并进行提交:

t = conn.begin()
try:
conn.execute(RAW_SQL)
t.commit()
except:
t.rollback()

PD:您也可以将 execution_options 放在 create_engine 参数中。

关于python - 使用 connection.execute() 的 sqlalchemy 原始 sql 查询限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11222223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com