-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_excel_to_mysql.py
66 lines (52 loc) · 1.7 KB
/
export_excel_to_mysql.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
# coding=utf-8
import xlrd
import MySQLdb
book = xlrd.open_workbook(u"/home/mobvoi/Downloads/微信公众号.xlsx")
sheet = book.sheet_by_index(1)
# 建立mysql连接
database = MySQLdb.connect(host='127.0.0.1', user='root', passwd='******', db='pitaya', port=3306,
use_unicode=True, charset="utf8")
# 获得游标对象, 用于逐行遍历数据库数据
cursor = database.cursor()
# 创建插入SQL语句
query = """
insert into tag_main (name,domain,level,parent_id) values(%s,%s,%s,%s)
"""
quer1 = """select id from tag_main where name = %s"""
def excel_insert_data():
i = 0
listh = []
# 创建一个for循环迭代读取xls文件每行数据的, 从第二行开始是要跳过标题
for rx in range(0, sheet.ncols, 2):
id = i
uri = sheet.cell(1, rx).value
values = (uri, "public_account", "1", "0")
cursor.execute(query, values)
i = i + 1
listh.append(uri)
for rx in range(1,sheet.ncols,2):
id2 = i
for e in range(1,sheet.nrows,1):
tag_name = sheet.cell(e,rx).value
if tag_name in listh:
break
pa = sheet.cell(1,rx -1).value
cursor.execute("select id from tag_main where name ='%s'" % pa)
parent_id = cursor.fetchone()
va = (tag_name,"public_account","2",parent_id)
cursor.execute(query,va)
i = i + 1
listh.append(tag_name)
def excel_close():
# 关闭游标
cursor.close()
# 提交
database.commit()
# 关闭数据库连接
database.close()
def main():
excel_insert_data()
excel_close()
print u"Done!恭喜"
if __name__ == "__main__":
main()