python写入数据到timesten

环境:

Python:3.6.5

Timesten:18.1.4

 

#!/usr/bin/env python
#coding=utf-8
import os, json, urllib, datetime, shutil,random,uuid
from random import choice
import traceback
import time

import cx_Oracle
import pyodbc




##timestens表结构
###CREATE TABLE app_message_all (
###  message_id number NOT NULL PRIMARY KEY,
###  user_id number NOT NULL,
###  sender_seq_no varchar2(45) DEFAULT NULL,
###  title varchar2(100) DEFAULT NULL,
###  message varchar2(500) DEFAULT NULL,
###  extra varchar2(2048) DEFAULT NULL,
###  send_date date default sysdate not null,
###  status number DEFAULT 0 NOT NULL,
###  message_type number  DEFAULT 0  NOT NULL,
###  massive_type number  DEFAULT 0 NOT NULL,
###  deleted number DEFAULT 0 NOT NULL,
###  create_time date DEFAULT sysdate NOT NULL
###);
###
###create index idx_sender_seq_no on app_message_all(sender_seq_no);
###create index idx_user_id_msg_type on app_message_all(user_id,message_type);
###create index idx_user_id_status on app_message_all(user_id,status);
###create index idx_user_massive on app_message_all(user_id,massive_type);
###create index idx_create_time on app_message_all(create_time);


def insert_data_message01():
    db = cx_Oracle.connect("hxl", "oracle", "mytest",encoding="UTF-8")
    # 得到一个可以执行SQL语句的光标对象
    cursor = db.cursor()

    db.autocommit=False ## 关闭字自动提交

    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]

    for i in range(1, 1000001):
        str_i = str(i)
        curr_time = datetime.datetime.now()
        time_str = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')
        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 = '回族镇卫生院有最新通知,请点击查看' + str_i

        l_message = '亲爱的' + str_i + '家长,【自2022年8月1日起,门诊接种日变为每周二、四、六为接种日,望大家相互转告,谢谢配合,勿跑空。】xxxx镇卫生院电话:xxxxx。'
        l_extra = '{"hospitalId":6347,"vchildCode":"xxx032820161xxx6000xxxx","showMessage":"亲爱xxxx家长,【自2022年8月1日起,门诊接种日变为每周二、四、六为接种日,望大家相互转告,谢谢配合,勿跑空。】xxx镇卫生院电话:xxxxxx。","messageTypeTitle":"门诊通知","childId":19509955,"templateId":36}'
        l_send_date = str(curr_time)
        l_deleted = 0

        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')"%(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:
            # 执行sql语句
            cursor.execute(insert_sql)
            # 提交到数据库执行
            if (i % 2) == 0:
                db.commit()
        except Exception as err:
            # Rollback in case there is any error
            print("sql语句执行错误", err)
            db.rollback()
    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_message01()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

db = cx_Oracle.connect("hxl", "oracle", "mytest",encoding="UTF-8")
这里的mytest配置请参考:https://www.cnblogs.com/hxlasky/p/17451916.html

 

posted @ 2023-06-02 17:23  slnngk  阅读(20)  评论(0编辑  收藏  举报