· 8 years ago · Jun 21, 2018, 03:42 PM
1
2import sqlite3
3
4
5foobar_uri = 'file:foobar_database.db?mode=memory&cache=shared'
6not_really_foobar_uri = 'file:not_really_foobar?mode=memory&cache=shared'
7
8# connect to databases in no particular order
9db2 = sqlite3.connect(foobar_uri)
10db_lol = sqlite3.connect(not_really_foobar_uri)
11db1 = sqlite3.connect(foobar_uri)
12
13# create cursor as db2
14cur2 = db2.cursor()
15
16# create table as db2
17db2.execute('CREATE TABLE IF NOT EXISTS foo (NUMBER bar)')
18
19# insert values as db1
20db1.execute('INSERT INTO foo VALUES (42)')
21db1.commit()
22
23# and fetch them from db2 through cur2
24cur2.execute('SELECT * FROM foo')
25db2.commit()
26print(cur2.fetchone()[0]) # 42
27
28# test that db_lol is not shared with db1 and db2
29try:
30 db_lol.cursor().execute('SELECT * FROM foo')
31except sqlite3.OperationalError as exc:
32 print(exc) # just as expected