forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_mysql.py
More file actions
34 lines (28 loc) · 968 Bytes
/
Copy pathstore_mysql.py
File metadata and controls
34 lines (28 loc) · 968 Bytes
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
# -*- coding: utf-8 -*-
import mysql.connector
def store_mysql(filepath):
conn = mysql.connector.connect(user = 'root', password = '1207', database = 'ShowMeTheCode')
cursor = conn.cursor()
# 判断表是否已经存在
cursor.execute('show tables in ShowMeTheCode;')
tables = cursor.fetchall()
findtables = False
for table in tables:
if 'code' in table:
findtables = True
if not findtables:
cursor.execute('''
CREATE TABLE `ShowMeTheCode`.`code` (
`id` INT NOT NULL AUTO_INCREMENT,
`code` VARCHAR(10) NOT NULL,
PRIMARY KEY (`id`));
''')
f = open(filepath, 'rb')
for line in f.readlines():
code = line.strip()
cursor.execute("insert into ShowMeTheCode.code (code) values(%s);", [code])
conn.commit()
cursor.close()
conn.close()
if __name__ == '__main__':
store_mysql('Activation_code.txt')