ORA-01036: python 中的非法变量 name/number
ORA-01036: illegal variable name/number in python
enter image description here 我正在尝试通过 python 将 csv 文件插入数据库。数据库是 oracle 。下面是我的代码 csv 文件名是 PatientStatus,Table 名称是 AIW1
将 cx_Oracle 导入为 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
回溯(最近调用最后)
C:\Users\SOFTWA~1.SUP\AppData\Local\Temp/ipykernel_956/3684779769.py 中
21 '''
22 游标 = conn.cursor()
---> 23 cursor.executemany(sql_insert, 记录)
24 cursor.commit();
25 打印('Task is complete.')
DatabaseError: ORA-01036: illegal variable name/number
Thanks in advance
在 Oracle 中,您需要使用不同的绑定占位符语法,而不是“?”。
查看 cx_Oracle 文档以获取有效加载数据的示例,请参阅 Batch Statement Execution and Bulk Loading。如果您仍然想要使用 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()
enter image description here 我正在尝试通过 python 将 csv 文件插入数据库。数据库是 oracle 。下面是我的代码 csv 文件名是 PatientStatus,Table 名称是 AIW1 将 cx_Oracle 导入为 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
回溯(最近调用最后) C:\Users\SOFTWA~1.SUP\AppData\Local\Temp/ipykernel_956/3684779769.py 中 21 ''' 22 游标 = conn.cursor() ---> 23 cursor.executemany(sql_insert, 记录) 24 cursor.commit(); 25 打印('Task is complete.')
DatabaseError: ORA-01036: illegal variable name/number
Thanks in advance
在 Oracle 中,您需要使用不同的绑定占位符语法,而不是“?”。
查看 cx_Oracle 文档以获取有效加载数据的示例,请参阅 Batch Statement Execution and Bulk Loading。如果您仍然想要使用 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()