一、说明
安装达梦的驱动
pip install dmPython==2.5.5
参数接收那里,其他数据库都是用%,达梦要用?
二、源码
#coding=utf-8 import pymysql import dmPython import time import uuid#pip install dmPython==2.5.5#测试单表插入性能达梦try:start_time = time.time() # 记录开始时间connection1 = pymysql.connect(host='192.168.0.100', user='user', password='passwd', database='testdb', charset='gbk', port=3306)#达梦connection2 = conn = dmPython.connect(host='192.168.0.99', port=5237, user='user', password='pass' )# 操作数据库cursor1 = connection1.cursor() # 创建一个游标# 操作数据库cursor2 = connection2.cursor() # 创建一个游标# 定义SQL查询语句,使用%s作为参数占位符sql = "SELECT ID,NAME,CONTENT,CREATE_TIME FROM DB_TEST_T"for i in range(80):print("------i:",i)# 执行SQL查询cursor1.execute(sql)# 获取查询结果rows = cursor1.fetchall()data_to_insert = []for row in rows:# 插入数据到数据表的sql语句insert_data_sql = """insert into DB_TEST_T(ID, NAME, CONTENT, CREATE_TIME)values(?, ?, ?, ?);"""random_uuid = uuid.uuid4()data = (str(random_uuid), row[1], row[2], row[3] ) data_to_insert.append(data)batch_size = 50000for i in range(0, len(data_to_insert), batch_size):batch = data_to_insert[i:i + batch_size]# 批量插入cursor2.executemany(insert_data_sql, batch)connection2.commit() # 提交事务end_time = time.time() # 记录结束时间execution_time = end_time - start_time # 计算执行时间print(f"Function execution took {execution_time} seconds")except pymysql.Error as e:print(f'错误:,{e}')
完整测试代码获取:
(1)登录-注册:http://resources.kittytiger.cn/
(2)搜索:国产数据库oceanBbase,达梦,金仓与mysql数据库的性能对比