Python3之pymysql导入mysql

$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])
上一篇:MFC使用MsComm做串口通信


下一篇:js 如何生成一个不重复的ID的函数