擅长:python、mysql、java
<p>根据conny和shadowmatter给出的答案,下面的代码将在发出PRAGMA语句之前检查您是否正在使用SQLite3:</p>
<pre><code>from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlite3 import Connection as SQLite3Connection
@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record):
if isinstance(dbapi_connection, SQLite3Connection):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON;")
cursor.close()
</code></pre>