简体   繁体   中英

SQLite3 with flask in-memory mode

I am using sqlite3 in a flask app (actually connexion). I would like to stay in-memory but keep the db between the requests to the server. So it should be destroyed after server is killed

  • When I use sqlite3.connect(':memory:') the db is destroyed after each response
  • So I followed this approach In memory SQLite3 shared database python and run sqlite3.connect('file::memory:?cache=shared&mode=memory', uri=True) . But then, a file called file::memory:?cache=shared&mode=memory appears in the app root and does not disappear when I kill the server. When I start the server again, the db-init routine which creates the tables fails, because the tables are already created.

I tried this out on linux and Mac. Both have same behaviour. It seems like the db is saved to file instead of being mapped to memory. My python version is 3.9 and sqlite3.sqlite_version_info is (3, 37, 0)

Try connecting/creating the database like this:

sqlite3.connect(':memory:')

I am suspecting that sqlite is treating this 'file::memory:?cache=shared&mode=memory' as a file name. Therefore on execution, creates a database file with that "name", in it's root directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM