如何将值动态传递到SQL脚本



我有下面提到的表格,表格中的字段总数为:5->[ id,name,surname,age,country ],这5个字段将用csv文件中的数据加载

我的Oracle表结构:

create table test
(
id number,
name varchar2(50 char),
surname varchar2(50 char),
age number,
country varchar2(50 char)
)

文件中的数据如下所述:

1|Fred|Nurke|21|UK
2|Henry|Crun|24|US
3|FSD|Nue|22|BA
4|Hen|Cn|25|MA

问题:下面脚本中的这一行我想使其动态:data.append((line[0], line[1], line[2], line[3], line[4]))

目前,这一行采用了5个参数,它们只是列字段名

id - > line[0]
name -> line[1]
surname -> line[2]
age -> line[3]
country -> line[4]

如果我有另一个表,它的结构不同,有8个字段,那么我需要重新编写脚本,并用以下行替换:data.append((line[0], line[1], line[2], line[3], line[4] , line[5], line[6] ,line[7]))

他们的方法就像把整行作为字符串,然后把它传递给下面的脚本

my_field ='line[0], line[1], line[2], line[3], line[4] , line[5], line[6] ,line[7]'
data.append((my_field))  

以下代码不起作用并引发语法错误:data.append((my_field))

我需要附上一份报价单吗:data.append(('my_field'))

将数据插入表的SQL脚本:

import logging
import cx_Oracle
import csv
import sys
import os
insertQuery = "insert into test (id,name,surname,age,country) values (:1, :2, :3, :4, :5)"
oracleconnection = 'un/pw@localhost/orclpdb1'
my_separator = '|'
file_name = 'demo_2021.csv'
my_field = 'line[0], line[1], line[2], line[3], line[4]'
set_inputsizes_value = 'None, 50, 50, None, 50'
def insertdata(oracleconnection, file_name, my_separator, insertQuery, my_field,set_inputsizes_value):

if sys.platform.startswith("darwin"):
cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME") + "/Downloads/instantclient_19_8")

try:
con = cx_Oracle.connect(oracleconnection)
cur = con.cursor()

# Predefine the memory areas to match the table definition
cur.setinputsizes(set_inputsizes_value)

# Adjust the batch size to meet your memory and performance requirements
batch_size = 10000

with open(file_name, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=my_separator)
data = []
for line in csv_reader:
data.append((my_field))
if len(data) % batch_size == 0:
cur.executemany(sql, data)
data = []
if data:
cur.executemany(insertQuery, data)
con.commit()
except Exception as er:
print(er)
insertdata(oracleconnection, file_name, my_separator, insertQuery, my_field,set_inputsizes_value)

考虑以下示例(我们有两个具有不同大小列表的列表):

my_field_01 = ['one','two','three', 'four']
my_field_02 = ['one','two','three', 'four','five','six']

第一种情况(my_field_01=>4项):

data=[]
data.append([i for i in my_field_01])
print(data)
# Output:
[['one', 'two', 'three', 'four']]

第二种情况,(my_field_02=>6项):

data=[]
data.append([i for i in my_field_02])
print(data)
# Output:
[['one', 'two', 'three', 'four', 'five', 'six']]

对于您的具体情况,您可以这样做:

for line in csv_reader:
data.append([i for i in line])

相关内容

  • 没有找到相关文章

最新更新