-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_to_ms.py
More file actions
78 lines (72 loc) · 2.04 KB
/
sqlite_to_ms.py
File metadata and controls
78 lines (72 loc) · 2.04 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
75
76
77
78
import pymysql as pm
import sqlite3 as sqli
import db_tools as dt
import configparser
# dt.clear_db()
# dt.create_table()
ms_db = dt.connection()
ms_cursor = ms_db.cursor()
conf = configparser.ConfigParser()
conf.read('db.conf')
sqli_db = sqli.connect(conf['sqlite_db']['db'])
sqli_cursor = sqli_db.cursor()
# copy data in student table
print('start copy data in student.')
sqli_cursor.execute('PRAGMA table_info ("student");')
header_raw = sqli_cursor.fetchall()
headers = [row[1] for row in header_raw]
query_predix = 'INSERT INTO student ('
for header in headers:
query_predix += (header + ', ')
query_predix = query_predix.strip(' ')
query_predix = query_predix.strip(',')
query_predix += ') VALUES ('
sqli_cursor.execute('SELECT * FROM student;')
data_list = sqli_cursor.fetchall()
for data in data_list:
query = query_predix
for content in data:
query += '"' + content + '", '
query = query.strip(' ')
query = query.strip(',')
query += ');'
# print(query)
try:
ms_cursor.execute(query)
ms_db.commit()
except:
ms_db.rollback()
print('copy done.')
# copy data in room table
print('start copy data in room.')
# read the header of table room and construct a predix of query
sqli_cursor.execute('PRAGMA table_info ("room");')
header_raw = sqli_cursor.fetchall()
headers = [row[1] for row in header_raw]
headers[4] = 'exptime'
query_predix = 'INSERT INTO room ('
for header in headers:
query_predix += (header + ', ')
query_predix = query_predix.strip(' ')
query_predix = query_predix.strip(',')
query_predix += ') VALUES ('
# read the content in table
sqli_cursor.execute('SELECT * FROM room;')
data_list = sqli_cursor.fetchall()
# print(data)
for data in data_list:
query = query_predix
for content in data:
query += '"' + content + '", '
query = query.strip(' ')
query = query.strip(',')
query += ');'
# print(query)
try:
ms_cursor.execute(query)
ms_db.commit()
except:
ms_db.rollback()
print('copy done.')
ms_db.close()
sqli_db.close()