zoukankan      html  css  js  c++  java
  • python批量写入oracle

    python将数组数据批量写入oracle

    from datetime import datetime
    
    import cx_Oracle
    
    
    if __name__ == '__main__':
        # build rows for each date and add to a list of rows we'll use to insert as a batch
        rows = []
    
        for i in range(1, 5):
            row = (i * 1, i * 2, i * 3, i * 4, i * 5)
            rows.append(row)
    
        # insert all of the rows as a batch and commit
    
        # 测试表
        database_table_name = 'test_load1'
    
        ip = '你的IP'
        port = 1521
        SID = 'orcl12'
        dsn = cx_Oracle.makedsn(ip, port, SID)
    
        try:
            # 连接数据库
            connection = cx_Oracle.connect('SCOTT', '123', dsn)
            # 测试连接用——输出数据库版本
            print(connection.version)
            # 获取游标
            cursor = cx_Oracle.Cursor(connection)
            # 写入操作
            cursor.prepare('insert into ' + database_table_name + ' (C1, C2, C3, C4, C5) values (:1, :2, :3, :4, :5)')
            # 执行入库
            cursor.executemany(None, rows)
            connection.commit()
            cursor.close()
            connection.close()
    
        except Exception as e:
            print('Oracle 写入失败,Exception:{0}'.format(e))
            connection.rollback()
            connection.close()
    诸业皆是自作自受,休咎祸福,尽从心生。
  • 相关阅读:
    Python变量状态保持四种方法
    Python参数基础
    Django Form 表单
    Python开发第四篇
    Python开发第三篇
    设计模式(一)概述
    Python自学之路——自定义简单装饰器
    Python开发第二篇
    Python开发第一篇
    Python核心编程——多线程threading和队列
  • 原文地址:https://www.cnblogs.com/1394htw/p/14887036.html
Copyright © 2011-2022 走看看