-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60db_memory.py
More file actions
74 lines (61 loc) · 1.78 KB
/
60db_memory.py
File metadata and controls
74 lines (61 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import sqlite3
import random
import time
start = time.time()
mem = sqlite3.connect(":memory:")
con = sqlite3.connect("DBtesty/db01.sqlite")
bck = sqlite3.connect("DBtesty/db01.bck")
tableName = "t01"
# Load real DB file to in-memory DB
try:
with con:
con.backup(mem)
print("OK: db -> mem")
except sqlite3.Error as error:
print("Problem with db -> mem: ", error)
# Update / change records in in-memory DB
try:
with mem:
cur = mem.cursor()
query = "INSERT INTO {}(col01, col02) VALUES (?,?)".format(tableName)
newRecords = []
oneRecord = ()
for iter in range(2):
randomFloat = random.random()*100
oneRecord = (iter + time.time_ns(), randomFloat,)
newRecords.append(oneRecord)
# print(type(newRecords))
# print(type(newRecords[0]))
cur.executemany(query, newRecords)
print("Last rowid:", cur.lastrowid)
print("Modified rows:", cur.rowcount)
mem.commit
except sqlite3.Error as error:
print("Problem insert new records: ", error)
try:
with mem:
cur = mem.cursor()
query = "SELECT * FROM {} ORDER BY id DESC LIMIT 1".format(tableName)
result = cur.execute(query).fetchall()
print(result)
except sqlite3.Error as error:
print("Problem with simple query: ", error)
# In-memory DB backup to real DB file
try:
with mem:
mem.backup(con)
print("OK: mem -> db")
except sqlite3.Error as error:
print("Problem with mem -> db", error)
# Backup real DB file
try:
with con:
con.backup(bck)
print("OK: db -> bck")
except sqlite3.Error as error:
print("Problem with db -> bck: ", error)
end = time.time()
totalTime = end - start
print("---")
print(totalTime)