代码拉取完成,页面将自动刷新
"""
the box is in the pen.
spirit is willing but the flesh is weak.
从Excel文件中读取数据写入数据库
create database stock default character set utf8mb4;
use stock;
create table tb_baba_stock
(
stock_id bigint unsigned auto_increment comment '编号',
trade_date date not null comment '交易日',
high_price decimal(16, 6) not null comment '最高价',
low_price decimal(16, 6) not null comment '最低价',
open_price decimal(16, 6) not null comment '开盘价',
close_price decimal(16, 6) not null comment '收盘价',
trade_volume bigint unsigned not null comment '交易量',
primary key (stock_id)
);
grant insert, select on stock.* to 'guest'@'10.7.174.%';
"""
import openpyxl
import pymysql
wb = openpyxl.load_workbook('阿里巴巴2020年股票数据.xlsx')
ws = wb.active
params = []
for row_idx in range(2, ws.max_row + 1):
values = []
for col_idx in range(1, ws.max_column):
values.append(ws.cell(row_idx, col_idx).value)
params.append(values)
conn = pymysql.connect(host='10.7.174.103', port=3306,
user='guest', password='Guest.618',
database='stock', charset='utf8mb4')
try:
with conn.cursor() as cursor:
# 执行批量插入操作
cursor.executemany(
'insert into tb_baba_stock '
' (trade_date, high_price, low_price, open_price, close_price, trade_volume) '
'values '
' (%s, %s, %s, %s, %s, %s)',
params
)
conn.commit()
except pymysql.MySQLError as err:
print(err)
conn.rollback()
finally:
conn.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。