forked from SeryKK/PracticalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveDB.py
More file actions
37 lines (33 loc) · 1.1 KB
/
saveDB.py
File metadata and controls
37 lines (33 loc) · 1.1 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
import pymysql
import csv
def opmysql(data): # 插入数据库
table = 'JDBook'
keys = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
try:
cursor.execute(sql, tuple(data.values()))
connectPool.commit()
print('插入成功')
except:
connectPool.ping(True)
# 连接数据库
connectPool = pymysql.connect(host="localhost", user="root", password="123456", database="jdbook", charset="utf8")
cursor = connectPool.cursor()
def read_csv():
with open('Top10.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
head_row = next(reader)
booklist = []
for item in reader:
bookdict = {}
bookdict['id'] = int(item[0])
bookdict['name'] = item[1]
bookdict['price'] = float(item[2])
bookdict['public'] = item[3]
booklist.append(bookdict)
print(booklist)
return booklist
booklist = read_csv()
for item in booklist:
opmysql(item)