当前位置: 首页 > news >正文

Python,pymysql,create database,table,execute insert

python -m pip install pymysql -i https://pypi.tuna.tsinghua.edu.cn/simple
import uuid
import time
import threading
from datetime import datetime
import pymysqlTOTAL_ROWS=10**9
BATCH_SIZE=100000
THREAD_NUM=8total_inserted=0
batch_completed=0
progress_lock=threading.Lock()config={'host':'127.0.0.1','user':'root','password':'root','port':3306
}class Book:def __init__(self,id,name,isbn,author,comment,content,summary,title,topic):self.id=idself.name=nameself.isbn=isbnself.author=authorself.comment=commentself.content=contentself.summary=summaryself.title=titleself.topic=topicdef __str__(self):return f'Id:{self.id},Name:{self.name},ISBN:{self.isbn},Author:{self.author},Comment:{self.comment},Content:{self.content},Summary:{self.summary},Title:{self.title},Topic:{self.topic}'def to_dict(self):return {'name':self.name,'isbn':self.isbn,'author':self.author,'comment':self.comment,'content':self.content,'summary':self.summary,'title':self.title,'topic':self.topic}def get_mysql_conn():try:conn=pymysql.connect(**config)print(f'{datetime.now()} connect to mysql successfully!')return connexcept Exception as ex:print(f'{datetime.now()} exception:{str(ex)}')def get_mysql_conn_cur(conn):try:cur=conn.cursor()print(f'{datetime.now()} connect to mysql successfully!')return curexcept Exception as e:print(f'{datetime.now()},exception {str(e)}')def close_conn():try:conn=get_mysql_conn()if conn.open:conn.close()print(f'{datetime.now()},mysql connection closed')except Exception as ex:print(f'{datetime.now()} exception:{str(ex)}')def execute_sql(conn,cur,sql_str=""):try: if cur is None or conn is None:error_msg="cursor or connection is null!"print(f'{datetime.now()},{error_msg}')return Noneif not sql_str.strip():error_msg="sql_str is empty!"print(f'{datetime.now()},{error_msg}')return Nonecur.execute(sql_str)conn.commit()result=cur.fetchone()if result is None:result=cur.rowcountprint(f'{datetime.now()} execute {sql_str} successfully,affected rows:{result}')else:print(f'{datetime.now()} executed {sql_str} successfully,result:{result}')return resultexcept Exception as ex:if conn is not None:conn.rollback()error_msg=f'exception:{str(ex)}'        print(f'{datetime.now()},exception:{str(ex)}') return Nonedef create_db(db_name="db"):try:conn=get_mysql_conn()cur=get_mysql_conn_cur(conn)sql=f"create database if not exists {db_name}"execute_sql(conn,cur,sql)print(f'{datetime.now()}, execute sql successfully!\n{sql}')except Exception as ex:print(f'{datetime.now()},exception:{str(ex)}')def create_table(db_name="db",table_name="t1"):try:conn=get_mysql_conn()cur=get_mysql_conn_cur(conn)use_sql=f"use {db_name}"execute_sql(conn,cur,use_sql)create_table_sql=f"""create table if not exists {table_name}(Id bigint not null auto_increment primary key,name varchar(1000) not null default '',isbn varchar(1000) not null default '',author varchar(1000) not null default '', comment text,content text,summary text,title varchar(1000) not null default '',topic varchar(1000) not null default '',create_time datetime not null default current_timestamp,update_time datetime not null default current_timestamp on update current_timestamp)engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci"""
        execute_sql(conn,cur,create_table_sql)except Exception as ex:print(f'{datetime.now()},{str(ex)}')def insert_book_list_into_db(cnt=10000):conn=Nonecur=Nonetry:book_list=[]arr=range(1,cnt+1)for a in arr:book_list.append(Book(a,f'Name_{a}',f'ISBN_{a}_{uuid.uuid4().hex}',f'Author_{a}',f'Comment_{a}',f'Content_{a}',f'Summary_{a}',f'Title_{a}',f'Topic_{a}'))print(f'{datetime.now()} generated {len(book_list)} rows data')insert_params=[(bk.to_dict()['name'],bk.to_dict()['isbn'],bk.to_dict()['author'],bk.to_dict()['comment'],bk.to_dict()['content'],bk.to_dict()['summary'],bk.to_dict()['title'],bk.to_dict()['topic'])for bk in book_list]insert_sql="insert into t1(name,isbn,author,comment,content,summary,title,topic) values (%s,%s,%s,%s,%s,%s,%s,%s)"config['db']='db'conn=pymysql.connect(**config)cur=conn.cursor()cur.executemany(insert_sql,insert_params)conn.commit()print(f'{datetime.now()} insert {cur.rowcount} rows into t1 in batch')except pymysql.MySQLError as ex:if conn:conn.rollback()print(f'{datetime.now()},pymysql.MySQLError: {str(ex)}')except Exception as ex:if conn:conn.rollback()print(f'{datetime.now()} {str(ex)}')finally:if cur:cur.close()if conn and conn.open:conn.close()print(f'{datetime.now()} database connection is closed!')def insert_batch(batch_id,start_id,end_id):global total_inserted,batch_completedconn=Nonecur=Nonetry:book_list=[]for a in range(start_id,end_id+1):book_list.append(Book(id=a,name=f'Name_{a}',isbn=f'ISBN_{a}_{uuid.uuid4().hex}',author=f'Author_{a}',comment=f'Comment_{a}',content=f'Content_{a}',summary=f'Summary_{a}',title=f'Title_{a}',topic=f'Topic_{a}'))print(f'{datetime.now()},[batch {batch_id}] generated {len(book_list)} rows data')insert_params=[(bk.to_dict()['name'],bk.to_dict()['isbn'],bk.to_dict()['author'],bk.to_dict()['comment'],bk.to_dict()['content'],bk.to_dict()['summary'],bk.to_dict()['title'],bk.to_dict()['topic'],)for bk in book_list]insert_sql="insert into t1(name,isbn,author,comment,content,summary,title,topic) values (%s,%s,%s,%s,%s,%s,%s,%s)"config['db']='db'conn=pymysql.connect(**config)cur=conn.cursor()cur.executemany(insert_sql,insert_params)conn.commit()affected_rows=cur.rowcountwith progress_lock:total_inserted+=affected_rowsbatch_completed+=1print(f'{datetime.now()} [batch:{batch_id}] inserted {affected_rows} rows and totally inserted {total_inserted}')except pymysql.MySQLError as ex:if conn:conn.rollback()print(f'{datetime.now()}, [batch {batch_id}] database exception! {str(ex)},and has rolled back!')except Exception as ex:if conn:conn.rollback()print(f'{datetime.now()}, [batch {batch_id}] execute failed!{str(ex)} and has rolled back!')finally:if cur:cur.close()if conn and conn.open:conn.close()def thread_worker(batch_range):start_batch,end_batch=batch_rangefor batch_id in range(start_batch,end_batch+1):start_id=(batch_id-1)*BATCH_SIZE+1end_id=batch_id*BATCH_SIZEinsert_batch(batch_id,start_id,end_id)time.sleep(0.1)def split_batch_to_threads():total_batch=TOTAL_ROWS//BATCH_SIZEif TOTAL_ROWS%BATCH_SIZE!=0:total_batch+=1print(f'{datetime.now()},begin to insert 10 billion data into database| total batch:{total_batch} | batch_size:{BATCH_SIZE}| parallel threads:{THREAD_NUM}')batch_per_thread=total_batch//THREAD_NUMthreads=[]for i in range(THREAD_NUM):start_bacth=i*batch_per_thread+1end_batch=(i+1)*batch_per_thread if i<THREAD_NUM-1 else total_batcht=threading.Thread(target=thread_worker,args=((start_bacth,end_batch),),name=f'InsertedThread-{i+1}')threads.append(t)t.start()print(f'{datetime.now()} thread start {t.name},batch:{start_bacth} ~ {end_batch}')for t in threads:t.join()print(f'{datetime.now()} all threads finished,totally inserted {total_inserted} rows data')if __name__=="__main__":create_db()create_table()start_total=time.time()split_batch_to_threads()end_total=time.time()total_seconds=end_total-start_totalprint(f'{datetime.now()},totally inserted {TOTAL_ROWS} data,time cost:{total_seconds} seconds')

 

 

use db;
show tables;
select * from t1 order by id desc limit 1;

 

 

image

 

http://www.jsqmd.com/news/339157/

相关文章:

  • Flutter for OpenHarmony 实战:网络监控登录系统完整开发指南
  • 用“内容+投放+运营”打出增长曲线
  • Flutter for OpenHarmony 进阶:Socket通信与网络编程深度解析
  • WiFi6随身WiFi是智商税,还是出行神器?一篇说清值不值得买
  • 用“产品力表达”撬动自然流量与高转化
  • Skill开发实战:从入门到精通,教你创建自动生成Skills的Skill,收藏必备!
  • 用 PyTorch 实现 CBOW 模型
  • 大模型RAG优化完全指南:17种策略详解与应用场景对照表,小白也能轻松上手!
  • 数字图像处理篇---高通滤波
  • 目标:覆盖全网主流公链,SYNBO 正式开启公链生态媒体合作矩阵计划
  • 从数学不及格到AI导师:非技术背景者的逆袭之路_30岁转行AI,可能吗?真实案例告诉你
  • 大模型网关:大模型时代的智能交通枢纽|得物技术
  • 2025AI大爆发:程序员小白如何抓住高薪风口?2025年AI就业薪资表曝光,大厂2倍薪酬抢人
  • DTS按业务场景批量迁移阿里云MySQL库实战(下):迁移管理平台设计与实现
  • 浅谈多元线性方程组
  • 跨境电商:从“跑量”到“跑赢利润”的一套打法
  • qwen2.5vl源码解析
  • AB实验高阶技法(五):卡方检验——搞懂适合度与独立性的本质区别
  • 脂质纳米颗粒(LNPs):mRNA 递送的核心非病毒载体及制备技术
  • LLM评估系统完全指南:从传统评估到Agent裁判,一篇就够了!
  • 【例9.18】合并石子(信息学奥赛一本通- P1274)从暴搜到区间 DP:石子合并的四种写法
  • 2026 寒假集训题目
  • JMeter启动时常见的错误
  • 7.blender修改器(制作螺母)
  • 测试员收到offer提了离职,却被告知背调不合格,背调究竟在调什么?
  • 一种多选项的高效存取(存储、查询)解决方案
  • Erlang 使用escript打包多个模块构建一个可执行文件
  • AI产品经理:大模型时代最有“钱“景的岗位,零基础入门到实战全攻略_想转行AI产品经理,90%的人第一步就走错了!
  • 计算机毕业设计springboot飞机票预订系统 基于Spring Boot的航空票务服务平台设计与实现 基于Java Web的民航订票管理系统开发
  • IS420UCSBH4A 产品概述