1 Star 0 Fork 0

jackfrued / db2103

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example06.py 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
jackfrued 提交于 2021-06-17 07:03 . Python接入MySQL数据库案例
"""
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()
1
https://gitee.com/jackfrued/db2103.git
git@gitee.com:jackfrued/db2103.git
jackfrued
db2103
db2103
master

搜索帮助