I'm trying to share a sqlite3 database between one writer process, and one reader process. However, it does not work, and it seems to me that nothing is being written in example.db.
reader.py
import sqlite3
from time import sleep
conn = sqlite3.connect('example.db', isolation_level=None)
c = conn.cursor()
while True:
c.execute("SELECT * FROM statistics")
try:
print '**'
print c.fetchone()
except:
pass
sleep(3)
writer.py
import sqlite3
from time import sleep
import os
if os.path.exists('example.db'):
os.remove('example.db')
conn = sqlite3.connect('example.db', isolation_level=None)
c = conn.cursor()
c.execute('PRAGMA journal_mode=wal')
print c.fetchone()
c.execute("CREATE TABLE statistics (stat1 real, stat2 real)")
stat1 = 0
stat2 = 0.0
while True:
stat1 += 1
stat2 += 0.1
cmd = "INSERT INTO statistics VALUES (%d,%f)" % (stat1, stat2)
print cmd
c.execute(cmd)
#conn.commit()
c.execute("PRAGMA wal_checkpoint=FULL")
sleep(0.25)
I run writer.py in a background process, and then I run reader.py. This is what it shows:
**
(1.0, 0.1)
**
(1.0, 0.1)
**
(1.0, 0.1)
(...)
What is the correct (and best) way to set this framework with one reader and one writer?
解决方案
What do you expect? You only select the first row of the table. And this is always the same row. The "PRAGMA"-statements have no visible effects here.