欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

示例:用python_dataset导入900w条数据到oracle

程序员文章站 2024-03-14 14:38:10
...

示例:python用dataset导入900w条数据到oracle

dataset可以批量导入数据库,以dict结构就可以导入数据库

import dataset
import logging
import time
def handle_txt():
    ids = []
    uids= []
    with open('./xxx.txt',mode='r',encoding="utf8") as f:
        rows = f.readlines()
        for row in rows:
            text = row.split(',')
            ids.append(text[0])
            uids.append(text[1])

    logging.StreamHandler(stream=None)
    db = dataset.connect('oracle://"用户名":“密码”@“数据库连接名”')
    table = db['tablename']


    import_data = []
    data = zip(ids,uids)
    data = list(data)
    for i in data :
        start_time = time.time()
        import_data.append(dict(id=i[0], uid=i[1]))
        if len(import_data) == 20000:
            table.insert_many(import_data)
            import_data = []
        end_time = time.time()
        cost_time = end_time - start_time
        logging.info("%s" % cost_time)

    import_data = []
    start_num = len(data) % 20000
    print(start_num)
    data_save = data[-start_num:]
    for i in data_save:
        import_data.append(dict(id=i[0], uid=i[1]))
        table.insert_many(import_data)
    db.commit()


if __name__ == '__main__':
    data= handle_txt()







相关标签: 示例