在此處輸入影像描述 我正在嘗試通過 python 將 csv 檔案插入資料庫。資料庫是 oracle 。下面是我的代碼 csv 檔案名是 PatientStatus 和表名是 AIW1 import cx_Oracle as cn import pandas as pd # pip install pandas
```
df = pd.read_csv('PatientStatus.csv')
columns = [ 'UNITNAME' ,
'PATIENTSTATUS', 'PATIENTCOUNTS', 'COUNTRY','STATENAME','CITYNAME']
df_data = df[columns]
records = df_data.values.tolist()
conn = cn.connect(user="", password="",dsn="")
sql_insert = '''
INSERT INTO AIW1
VALUES (?, ?, ?, ?, ?, ?, ?,?,?)
'''
cursor = conn.cursor()
cursor.executemany(sql_insert, records)
cursor.commit();
print('Task is complete.')
cursor.close()
conn.close()
```
I am getting the
DatabaseError
Traceback(最近一次通話最后一次)C:\Users\SOFTWA~1.SUP\AppData\Local\Temp/ipykernel_956/3684779769.py in 21 ''' 22 cursor = conn.cursor() ---> 23 cursor.executemany (sql_insert, 記錄) 24 cursor.commit(); 25 print('任務完成。')
DatabaseError: ORA-01036: illegal variable name/number
Thanks in advance
uj5u.com熱心網友回復:
在 Oracle 中,您需要使用不同的系結占位符語法,而不是“?”。
查看 cx_Oracle 檔案以獲取有效加載資料的示例,請參閱批處理陳述句執行和批量加載。如果您仍然想要使用 Pandas 的開銷,您可以將資料調整為類似的格式。
import cx_Oracle
import csv
# Predefine the memory areas to match the table definition.
# This can improve performance by avoiding memory reallocations.
# Here, one parameter is passed for each of the columns.
# "None" is used for the ID column, since the size of NUMBER isn't
# variable. The "25" matches the maximum expected data size for the
# NAME column
cursor.setinputsizes(None, 25)
# Adjust the number of rows to be inserted in each iteration
# to meet your memory and performance requirements
batch_size = 10000
with open('testsp.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
sql = "insert into test (id,name) values (:1, :2)"
data = []
for line in csv_reader:
data.append((line[0], line[1]))
if len(data) % batch_size == 0:
cursor.executemany(sql, data)
data = []
if data:
cursor.executemany(sql, data)
con.commit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484735.html
上一篇:如何取消管道行?
下一篇:如何處理存盤程序中的逗號分隔輸入
