国产数据库oceanBbase,达梦,金仓与mysql数据库的性能对比 四、python读mysql写入达梦数据库

一、说明

安装达梦的驱动

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 = 50000
        for 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数据库的性能对比

相关推荐
c8i7 分钟前
drf初步梳理
python·django
每日AI新事件7 分钟前
python的异步函数
python
这里有鱼汤1 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook10 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室11 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三12 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271116 小时前
Python之语言特点
python
刘立军16 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机19 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机21 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构