executemany例子

 

 

#!/usr/bin/env python
#coding=utf-8
import os, json, urllib, datetime, shutil,random,uuid
from random import choice
import pymysql
import traceback
import time
gl_mysql_server = "192.168.1.183"
gl_user_name = "root"
gl_password = "mysql"
gl_db_name_msg="db_pushmsg"
gl_port=4000

def insert_data_message():

    db = pymysql.connect(host=gl_mysql_server,
                         port=gl_port,
                         user=gl_user_name,
                         password=gl_password,
                         database=gl_db_name_msg,
                         charset='utf8')
    # 得到一个可以执行SQL语句的光标对象
    cursor = db.cursor()
    ##db.autocommit(0) ## 关闭字自动提交
    ip_list = ['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6', '192.168.1.7','192.168.1.8', '192.168.1.9', '192.168.1.10', '192.168.1.11', '192.168.1.12', '192.168.1.13', '192.168.1.14', '192.168.1.15']
    message_type=[0,2,3,6,8,9,16,4096,8196,8448,12292,16385,16386,16387,32770,65536,69632,73728,77824]
    massive_type=[0,1000,1001,1002,1003,1004,1005,1006,1007]
    status = [1000,2000]
    insert_sql = "insert into app_message_all(user_id,message_type,massive_type,status,create_time,message_id,sender_seq_no,title,message,extra,send_date,deleted ) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on duplicate key update create_time=values(create_time)"
    data2 = []
    for j in range(1,501):
        data2 = []
        for i in range(1, 10001):
            str_i = str(i)
            curr_time = datetime.datetime.now()
            l_user_id=random.randint(10000000, 99999999)
            l_message_type = choice(message_type)
            l_massive_type = choice(massive_type)
            l_status = choice(status)
            l_create_time = str(curr_time)
            l_message_id=random.randint(1000000000000000, 9999999999999999)
            l_sender_seq_no=str(uuid.uuid4())
            l_title='test'+str_i
            l_message=test'
            l_extra='test'
            l_send_date =str(curr_time)
            l_deleted=0
            data2.append((l_user_id,l_message_type,l_massive_type,l_status,l_create_time,l_message_id,l_sender_seq_no,l_title,l_message,l_extra,l_send_date,l_deleted))
        try:
            cursor.executemany(insert_sql, data2)
        except Exception as e:
            print(e)
        db.commit()
    cursor.close()
    db.close()
    return 0



if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    ##l_flag = insert_data_for_doris()
    l_flag=insert_data_message()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

posted @ 2023-05-31 11:09  slnngk  阅读(31)  评论(0编辑  收藏  举报