-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_backup.py
53 lines (44 loc) · 1.06 KB
/
db_backup.py
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
"""
db_backup.py
Used to backup all of the data in the current database, make schema changes, and then reinput data
"""
import sqlite3 as lite
def getName():
"""
get current db version and then update it
"""
f = open('database_version.txt', 'r')
version = f.read()
f = open('database_version.txt', 'w')
f.close()
db_name = 'reviews' + str(version) + '.sqlite'
# update version
version = int(version)
version += 1
f.write(str(version))
f.close()
return db_name
db_name = 'reviews.sqlite'
def getTables():
conn = lite.connect(db_name)
with conn:
c = conn.cursor()
sql = 'SELECT * FROM sqlite_master WHERE type="table"'
c.execute(sql)
x = c.fetchall()
table_list = []
for i in xrange(len(x)):
table_list.append(x[i][1])
return table_list
def table_schema():
tables = getTables()
table_number = len(tables)
data = []
conn = lite.connect(db_name)
with conn:
c = conn.cursor()
for i in xrange(table_number): # get schema of each table
sql = 'SELECT * FROM ' + str(tables[i])
c.execute(sql)
data.append(c.fetchall())
print data