Sqlalchemy raw sql limit using connection.execute ()

This python code should run the statements in the database, but the sql statements are not executed:

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()

The file test.sqlcontains SQL statements that create 89 tables.

Tables are not created if I specify 89 tables, but if I reduce the number of tables to 2, this will work.

Is there a limit on the number of queries that can be executed in conn.execute? How to execute any number of raw requests like this?

+5
source share
2 answers

Perhaps forcing auto-commutation:

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

Another approach is to use transactions and commit:

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

PD: You can also put execute_options parameters into create_engine parameters.

+7

All Articles