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

时间:2024-11-20 13:07:14

一、说明

      安装达梦的驱动

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