仅作为代码笔记
import pymysql
db = pymysql.connect('127.0.0.1', 'root', 'root', 'devil')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
s = 'select count(*) FROM GOLD'
cursor.execute(s)
count = cursor.fetchall()[0][0]
print("""插入前总共:%d 条数据'""" % count)
for item in price_array:
price = item['price']
date = item['date']
sql = """INSERT INTO GOLD(price,date) VALUES (%f,'%s')""" % (price / 100, date)
try:
cursor.execute(sql)
except Exception:
continue
# 关闭数据库连接
db.commit()
s = 'select count(*) FROM GOLD'
cursor.execute(s)
count = cursor.fetchall()[0][0]
# cursor.
print("""插入前总共:%d 条数据 """ % count)
cursor.close()
db.close()
网友评论