Save / load in-memory DB with python sqlite3

As sqlite3.connect (': memory:') The DB image created in memory is You can save it by making it a dump file.

In the sample below, the dump file is stored as gzip.

python


#Load from file
con = sqlite3.connect(':memory:')
fp = gzip.open('./dump.sql.gz' ,'rb')
con.executescript(fp.read())
fp.close()

#Save to file
fp = gzip.open('./dump.sql.gz','wb')
for line in con.iterdump():
  fp.write('%s\n' % line)
fp.close()

However, if the number of cases increases with this method, It is not practical because it takes a long time to load.

Practically without using sqlite3.connect (': memory:') You should put the db file in tmpfs.

Recommended Posts

Save / load in-memory DB with python sqlite3
[Python] [SQLite3] Operate SQLite with Python (Basic)
Save & load data with joblib, pickle
Easily handle lists with python + sqlite3
Easily handle databases with Python (SQLite3)
Load gif images with Python + OpenCV
Load Mac Python import MySQL db
Send image with python, save with php
Save video frame by frame with Python OpenCV
Python / numpy> list (numpy array) file save / load
Put protocol buffers into sqlite with python
Trying to handle SQLite3 with Python [Note]
Record with Python → Save file (sounddevice + wave)
Load AWS-RDS / PostgreSQL tables with AWS-Lambda + Python
I tried hundreds of millions of SQLite with python
FizzBuzz with Python3
Scraping with Python
Load the network modeled with Rhinoceros in Python ③
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Load the network modeled with Rhinoceros in Python ②
Sqlite in python
Bingo with python
Zundokokiyoshi with python
Load the network modeled with Rhinoceros in Python ①
Excel with Python
Microcomputer with Python
Cast with python
Procedure to load MNIST with python and output to png
Specific sample code for working with SQLite3 in Python
Try to operate DB with Python and visualize with d3
Use Cursur that closes automatically with sqlite3 in Python
[Python] Added DB instance of RDS with Elastic Beanstalk
[Linux] [Python] [Pandas] Load Microsoft Access database (* .mdb) with Pandas
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Save memory with `` __slots__``
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER