$cat insert.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys
import datetime
import pymysql
def read_insert(filename, slot_type_id):
file =
open(filename)
i =
0
j =
0
list =
[]
db = pymysql.connect(host=
"test", user=
"test", passwd=
"test", port=3306, db=
"test", charset=
'utf8')
cursor =
db.cursor()
sql =
"insert into test(slot_type_id, type, value, gmt_modify, gmt_create) values (%s, %s, %s, %s, %s)"
for line
in file:
data = (slot_type_id,
"const", (line.replace(
"\n",
"")),
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
list.append(data)
if i > 5000
:
cursor.executemany(sql, list)
db.commit()
print(
"插入", j)
i =
0
list =
[]
i = i + 1
j = j + 1
if i >
0:
cursor.executemany(sql, list)
db.commit()
db.close()
file.close()
print(
"ok")
if __name__ ==
"__main__":
read_insert(sys.argv[1], sys.argv[2])
转载于:https://www.cnblogs.com/zhzhang/p/7308494.html
相关资源:垃圾分类数据集及代码
转载请注明原文地址: https://win8.8miu.com/read-1449460.html