In order to see if SQLite can be used by 2 processes at the same time, I tried this:
script1.py (updating the database every 1 second)
import sqlite3, time
conn = sqlite3.connect('test.db')
conn.execute("CREATE TABLE IF NOT EXISTS kv (key text, value text)")
for i in range(1000):
conn.execute('REPLACE INTO kv (key, value) VALUES (?,?)', (1, i))
conn.commit()
print i
time.sleep(1)
script2.py (querying the database every 1 second)
import sqlite3, time
conn = sqlite3.connect('test.db')
c = conn.cursor()
while True:
c.execute('SELECT value FROM kv WHERE key = ?', (1,))
item = c.fetchone()
print item
time.sleep(1)
I started script1.py
and then script2.py
, and let them running at the same time. I hoped that script2.py
would know (I don't know how though!) that the DB has been updated, and that it has to reload a part of it. But sadly I get this in script2.py
:
(u'0',)
(u'0',)
(u'0',)
(u'0',)
(u'0',)
(u'0',)
(u'0',)
i.e. it doesn't get script1.py
's updates.
Is there a simple way to make this work with SQLite?