sqlalchemy and SQLite shared cache

后端 未结 2 676
庸人自扰
庸人自扰 2021-02-05 08:13

SQLite supports a \"shared cache\" for :memory: databases when they are opened with a special URI (according to sqlite.org):

[T]he same in-me

2条回答
  •  滥情空心
    2021-02-05 08:37

    You should avoid passing uri=True on older Python versions and the problem will be fixed:

    import sqlite3
    import sys
    
    import sqlalchemy
    
    
    DB_URI = 'file::memory:?cache=shared'
    PY2 = sys.version_info.major == 2
    if PY2:
        params = {}
    else:
        params = {'uri': True}
    
    creator = lambda: sqlite3.connect(DB_URI, **params)
    
    engine = sqlalchemy.create_engine('sqlite:///:memory:', creator=creator)
    engine.connect()
    

提交回复
热议问题