如何使用 Python 以更快的方式将 csv 文件加载到 Oracle 表

我正在网上研究如何以更快的方式将 CSV 文件加载到 Oracle DB 表中

下面是我在文件中加载 1000 条记录并在短时间内加载的表的方式。

但是如果有 50,000 条记录,则需要大约 50,000 条记录。加载 5 分钟。

有没有更快更有效的方法将 csv 文件加载到 Oracle 表中?

我的代码:

import cx_Oracle
import csv

myquery='insert into TABLE (COLUMNNAME1,COLUMNNAME2,COLUMNNAME3,COLUMNNAME4,COLUMNNAME5,COLUMNNAME6) values (:1,:2,:3,:4,:5,:6)'
separator='|'

oracleConnection='user/password@hostIP/database'

def fileLoading(oracleconnection,file_name,myquery,separator):
    try:
        con = cx_Oracle.connect(oracleConnection)
        print("Connection established")
        reader = csv.reader(open("demp.csv","r"),header=None,delimiter=separator)
        columns = []
        for line in reader:
            columns.append(line)
            cur = con.cursor()
            for line in columns:
                print("Inserting record to table")
                insrt_stmt = myquery
                cur.execute(insrt_stmt,line)
                con.commit()
                cur.close()
                print("load completed")
    except Exception as er:
        print('ERRO:',er)
shiqing22 回答:如何使用 Python 以更快的方式将 csv 文件加载到 Oracle 表

我不熟悉 python,但我会尝试这样:

con = cx_Oracle.connect(oracleConnection)
print("Connection established")
reader = csv.reader(open("demp.csv","r"),header=None,delimiter=separator)
cur = con.cursor()
insrt_stmt = myquery

for line in reader:
   columns = []
   for line in columns:
       columns.append(line)
       cur.execute(insrt_stmt,line)
con.commit()
cur.close()
print("load completed")
,

使用 { "editor.wordWrap": "on","workbench.iconTheme": "eq-material-theme-icons","liveSassCompile.settings.formats": [ { "format": "expanded","extensionName": ".css","savePath": null } ] } ,如 documentation 中所示。这比重复调用 executeMany() 快得多:

execute()
本文链接:https://www.f2er.com/5519.html

大家都在问