-
Notifications
You must be signed in to change notification settings - Fork 153
/
cbdb.py
311 lines (254 loc) · 10.4 KB
/
cbdb.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# encoding=utf-8
"""
@author: SimmerChan
@github: https://github.com/SimmerChan
@zhihu: https://www.zhihu.com/people/simmerchan/
@wechat: 尘世美的小茶馆
@file: cbdb.py.py
@time: 2018/12/28 15:00
@desc: 将CBDB中的数据转为简体字后存入Mysql数据库中
简繁转换脚本traditional2simple是网上找的,具体作者不详。
"""
import sqlite3
import pymysql
from Chinese.structural_data.tradition2simple.traditional2simple import tradition2simple
import re
import traceback
from collections import OrderedDict, defaultdict
import pyodbc
def dict_factory(cursor, row):
"""
把sqlite中的记录转为字典
:param cursor:
:param row:
:return:
"""
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
# TODO 正则表达式过滤
chinese_pattern = re.compile(u'[\u4E00-\u9FA5]+')
question_mark_pattern = re.compile(u'\?')
illegal_pattern = re.compile(u'[^\u0000-\u9FA5]+')
space_pattern = re.compile(u' ')
# TODO 连接本地mysql的CBDB数据库
mysql_db = pymysql.connect(host="localhost", user="root", db="CBDB", use_unicode=True, charset="utf8mb4")
# TODO 用sqlite3读取CBDB数据库
sqlite_db_path = 'xxx'
sqlite_db = sqlite3.connect(sqlite_db_path)
row_factory = sqlite_db.row_factory
# TODO 用pyodbc链接CBDB的access数据库
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=E:\SimmerChan\lab\mywork\resources\20170829CBDBavBase\20170829CBDBavBase.mdb;'
)
cnxn = pyodbc.connect(conn_str)
crsr = cnxn.cursor()
mysql_cursor = mysql_db.cursor()
sqlite_cursor = sqlite_db.cursor()
# TODO 获取mysql保留关键词
mysql_cursor.execute('SELECT * FROM mysql.help_keyword')
reserved_keywords = [i[1] for i in mysql_cursor.fetchall()]
# TODO 获取所有表名
name_of_all_tables = [i[0] for i in sqlite_cursor.execute("select name from sqlite_master where type = 'table' order by name").fetchall()]
# TODO 获取所有表的主键和不含主键的表名
pk_of_table = defaultdict(list)
for table_name in name_of_all_tables:
for row in crsr.statistics(table_name, unique=True):
if row[5] is not None:
if row[5].replace(' ', '').lower().find('primarykey') != -1:
pk_of_table[table_name].append(row[8])
if len(pk_of_table[table_name]) == 0:
for row in crsr.statistics(table_name, unique=True):
pk_of_table[table_name].append(row[8])
for k, v in sorted(pk_of_table.items(), key=lambda item: item[0]):
if v[0] is not None:
pk_str = 'primary key ('
for pk in v:
pk_str += pk + ','
pk_str = pk_str[:-1] + ')'
pk_of_table[k] = pk_str
else:
del pk_of_table[k]
# TODO 记录有duplicate key的表
# table_with_dk = defaultdict(tuple)
name_of_table_with_PK = pk_of_table.keys()
print('Total tables which contains PK: {0}'.format(len(name_of_table_with_PK)))
for index, table_name in enumerate(name_of_table_with_PK):
print('{0}.Table {1} transferring...................'.format(index + 1, table_name))
# TODO 还原sqlite的row factory
sqlite_db.row_factory = row_factory
sqlite_cursor = sqlite_db.cursor()
# TODO 获取此表每个字段的类型
field_types = OrderedDict()
field_with_type = ''
fields_str = ''
values_str = ''
for i in sqlite_cursor.execute("PRAGMA TABLE_INFO({0})".format(table_name)).fetchall():
# TODO 给带空格的字段加下划线
field_name = i[1]
if field_name.upper() in reserved_keywords or space_pattern.search(field_name) is not None:
field_with_type += '`' + field_name + '`'
fields_str += '`' + field_name + '`,'
else:
field_with_type += field_name
fields_str += field_name + ','
values_str += '%s,' + ' '
if i[2] == u'CHAR':
field_types[field_name] = u'TEXT'
field_with_type += ' ' + u'TEXT' + ','
else:
field_types[field_name] = i[2]
field_with_type += ' ' + i[2] + ','
fields_str = fields_str[:-1]
values_str = values_str[:-2]
# TODO 在mysql中创建对应的表
field_with_type = field_with_type[:-1]
create_command = "create table {0} ({1}, {2}) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci".format(table_name, field_with_type, pk_of_table[table_name])
try:
mysql_cursor.execute(create_command)
except pymysql.err.InternalError:
print('{0}.Table {1} transferred successfully!\n'.format(index + 1, table_name))
continue
except pymysql.err.ProgrammingError:
traceback.print_exc()
print(create_command)
for k, v in field_types.items():
print(k, v)
exit()
# TODO 获取此表所有记录,将类型为char的字段值转为简体,存入mysql表中
sqlite_db.row_factory = dict_factory
sqlite_cursor = sqlite_db.cursor()
records = sqlite_cursor.execute("select * from {0}".format(table_name)).fetchall()
insert_command = "insert into {0} ({1}) values ({2})".format(table_name, fields_str, values_str)
values_list = list()
for record in records:
values = list()
for k, v in field_types.items():
if (v.find('CHAR') != -1 or v.find('TEXT') != -1) and record[k] is not None and chinese_pattern.search(record[k]) is not None:
values.append(tradition2simple(record[k]))
else:
values.append(record[k])
# try:
# mysql_cursor.execute(insert_command, values)
# except (pymysql.err.InternalError, pymysql.err.DataError):
# for v in values_list:
# print v
# print create_command
# print insert_command
# traceback.print_exc()
# exit()
#
# except pymysql.err.IntegrityError, e:
# # TODO 记录有duplicate key的表,查看是否是简繁转换造成的
# if table_name not in table_with_dk:
# table_with_dk[table_name] = e
# continue
values_list.append(tuple(values))
# mysql_db.commit()
try:
mysql_cursor.executemany(insert_command, values_list)
mysql_db.commit()
except (pymysql.err.InternalError, pymysql.err.DataError, pymysql.err.IntegrityError):
for v in values_list:
print(v)
print(create_command)
print(insert_command)
traceback.print_exc()
exit()
print('{0}.Table {1} transferred successfully!\n'.format(index + 1, table_name))
name_of_table_without_PK = list()
for n in name_of_all_tables:
if n not in name_of_table_with_PK:
name_of_table_without_PK.append(n)
print('Total tables which doesn\'t contain PK: {0}'.format(len(name_of_table_without_PK)))
for index, table_name in enumerate(name_of_table_without_PK):
print('{0}.Table {1} transferring...................'.format(index + 1, table_name))
# TODO 还原sqlite的row factory
sqlite_db.row_factory = row_factory
sqlite_cursor = sqlite_db.cursor()
# TODO 获取此表每个字段的类型
field_types = OrderedDict()
field_with_type = ''
fields_str = ''
values_str = ''
for i in sqlite_cursor.execute("PRAGMA TABLE_INFO({0})".format(table_name)).fetchall():
# TODO 给带空格的字段加下划线
field_name = i[1]
if field_name.upper() in reserved_keywords or space_pattern.search(field_name) is not None:
field_with_type += '`' + field_name + '`'
fields_str += '`' + field_name + '`,'
else:
field_with_type += field_name
fields_str += field_name + ','
values_str += '%s,' + ' '
if i[2] == u'CHAR':
field_types[field_name] = u'TEXT'
field_with_type += ' ' + u'TEXT' + ','
else:
field_types[field_name] = i[2]
field_with_type += ' ' + i[2] + ','
fields_str = fields_str[:-1]
values_str = values_str[:-2]
# TODO 在mysql中创建对应的表
field_with_type = field_with_type[:-1]
create_command = "create table {0} ({1}) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci".format(table_name, field_with_type)
try:
mysql_cursor.execute(create_command)
except pymysql.err.InternalError:
print('{0}.Table {1} transferred successfully!\n'.format(index + 1, table_name))
continue
except pymysql.err.ProgrammingError:
traceback.print_exc()
print(create_command)
for k, v in field_types.items():
print(k, v)
exit()
# TODO 获取此表所有记录,将类型为char的字段值转为简体,存入mysql表中
sqlite_db.row_factory = dict_factory
sqlite_cursor = sqlite_db.cursor()
records = sqlite_cursor.execute("select * from {0}".format(table_name)).fetchall()
insert_command = "insert into {0} ({1}) values ({2})".format(table_name, fields_str, values_str)
values_list = list()
for record in records:
values = list()
for k, v in field_types.items():
if (v.find('CHAR') != -1 or v.find('TEXT') != -1) and record[k] is not None and chinese_pattern.search(record[k]) is not None:
values.append(tradition2simple(record[k]))
else:
values.append(record[k])
# try:
# mysql_cursor.execute(insert_command, values)
# except (pymysql.err.InternalError, pymysql.err.DataError):
# for v in values_list:
# print v
# print create_command
# print insert_command
# traceback.print_exc()
# exit()
#
# except pymysql.err.IntegrityError, e:
# # TODO 记录有duplicate key的表,查看是否是简繁转换造成的
# if table_name not in table_with_dk:
# table_with_dk[table_name] = e
# continue
values_list.append(tuple(values))
# mysql_db.commit()
try:
mysql_cursor.executemany(insert_command, values_list)
mysql_db.commit()
except (pymysql.err.InternalError, pymysql.err.DataError, pymysql.err.IntegrityError):
for v in values_list:
print(v)
print(create_command)
print(insert_command)
traceback.print_exc()
exit()
print('{0}.Table {1} transferred successfully!\n'.format(index + 1, table_name))
# print 'Duplicate Table name with example error message:\n'
# for k, v in table_with_dk.items():
# print k, v
# TODO 关闭数据库连接
mysql_db.close()
sqlite_db.close()