python 连接神通数据库插入随机假数据

下载所需要的依赖包:

pip3 install JayDeBeApi  -f https://download.pytorch.org/whl/torch_stable.html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pip3 install Jpype1 -f https://download.pytorch.org/whl/torch_stable.html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pip3 install faker -f https://download.pytorch.org/whl/torch_stable.html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

代码如下: 

import jaydebeapi
from faker import Faker


def generate_fake_data():
    fake = Faker()
    data = [
        (fake.random_int(), fake.word(), fake.random_int(), fake.random_int(), fake.word(), fake.word(), fake.word(),
         fake.random_int())
        for _ in range(10)  # 生成10条随机数据,你可以根据需要调整数量
    ]
    return data


def insert_fake_data(conn):
    cursor = conn.cursor()
    fake_data = generate_fake_data()

    sql_insert = '''
    INSERT INTO SYSDBA.TEST_testtable1 (topic_id, topic_name, dir_id, parent_id, parent_ids, dir_name, origin_table_name, data_number)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    '''

    cursor.executemany(sql_insert, fake_data)
    conn.commit()
    cursor.close()


if __name__ == '__main__':
    url = 'jdbc:oscar://localhost:2003/osrdb'
    user = 'sysdba'
    password = 'szoscar55'
    driver = 'com.oscar.Driver'
    #驱动所在位置
    jar_file = 'E:\ShentongDatabase\jdbc\oscarJDBC16.jar'

    conn = jaydebeapi.connect(driver, url, [user, password], jar_file)
    insert_fake_data(conn)
    conn.close()

猜你喜欢

转载自blog.csdn.net/qq_44540985/article/details/134800825
今日推荐