论文详情存储mysql代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import traceback
import pymysql
import pymysql.cursors
import time
# 获得conn cursor
def get_conn():
# 打开数据库连接
conn = pymysql.connect(host='localhost',
user='root',
passwd='123',
db='text3',
port=3306,
charset='utf8')
# 创建游标,默认是元组型
cursor = conn.cursor()
return conn, cursor
# 关闭conn cursor
def close_conn(conn, cursor):
if cursor:
cursor.close()
if conn:
conn.close()
def query(sql, *args):
conn, cursor = get_conn()
cursor.execute(sql, args)
res = cursor.fetchall()
close_conn(conn, cursor)
return res
def insert_item(item):
cursor = None
conn = None
try:
print(f"{time.asctime()}开始插入历史数据")
conn, cursor = get_conn()
sql = "insert into homework values(%s,%s,%s,%s,%s,%s)"
for v in item:
cursor.execute(sql, [v[0], v[1], v[2], v[3], v[4], v[5]]) # %Y-%m-%d %H:%M:%S
conn.commit()
print(f"{time.asctime()}插入历史数据完毕")
except:
traceback.print_exc()
finally:
close_conn(conn, cursor)