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()