How to set SQLite PRAGMA statements with SQLAlchemy
How about using events:
from sqlalchemy.engine import Enginefrom sqlalchemy import event@event.listens_for(Engine, "connect")def set_sqlite_pragma(dbapi_connection, connection_record): cursor = dbapi_connection.cursor() cursor.execute("PRAGMA journal_mode=WAL") cursor.close()
See http://docs.sqlalchemy.org/en/rel_0_9/dialects/sqlite.html#foreign-key-support
Basically you should be able to rewrite the examples about foreignkey to achieve what you want. Take a look at https://stackoverflow.com/a/7831210/1890086
engine = create_engine(database_url)def _fk_pragma_on_connect(dbapi_con, con_record): dbapi_con.execute('PRAGMA journal_mode = MEMORY') # ...from sqlalchemy import eventevent.listen(engine, 'connect', _fk_pragma_on_connect)
Two previous solutions did not work, so I have found the another one.
from sqlalchemy.interfaces import PoolListenerclass MyListener(PoolListener): def connect(self, dbapi_con, con_record): dbapi_con.execute('pragma journal_mode=OFF') dbapi_con.execute('PRAGMA synchronous=OFF') dbapi_con.execute('PRAGMA cache_size=100000')engine = create_engine('sqlite:///' + basefile,echo=False, listeners= [MyListener()])