将csv加载到Oracle

时间:2017-07-04 01:19:38

标签: python python-2.7 csv cx-oracle

我无法将csv数据导入Oracle表。

  

ERROR:

    Traceback (most recent call last):
    File "H:/Standard_Procedures/Dataset_Management/Table_to_Oracle.py", line 139, in <module>
      sde_cursor.execute(insert_string,row)
    DatabaseError: ORA-01747: invalid user.table.column, table.column, or column specification

CODE:

with open(dataset) as f:
    reader = csv.reader(f, delimiter=',')
    column_string = ','.join(next(reader)).translate(None,'"')
    insert_string='INSERT INTO {} ({}) VALUES ('.format(dataset_name, column_string)
    val_list=[]
    for i in range(1,len(first_row)+1):
        val_list.append(':'+ str(i))
    value_string=','.join(val_list)
    insert_string += value_string + ')'
    for row in reader:
        print 'Insert String = {}'.format(insert_string)
        print 'Row = {}'.format(row)
        sde_cursor.execute(insert_string,row)

打印结果:

插入String = INSERT INTO area_harvest_summary_20170629a(PERIOD,YEAR,PERIODWIDTH,ObjectID,TSA_UKEY,POLY_NUM,CLUSTER,FMU,LANDBASE,WATERSHED,COM​​PARTMENT,OWNERSHIP,AREA,YC,Cover,AgeAtHarvest,HarvestYear,Treatment,NewYc,PriConifer, PriDecid,IncConifer,IncDecid,ConiferPieceSize,DecidPieceSize)VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13 ,:14,:15,:16,:17,:18,:19,:20,:21,:22,:23,:24,:25)

Row = ['1','10','10','A14029867','7025115','851840750','A14010716','A14','CON','480','A14-A ','Northlan','9.022459984','5','C_Mx_Sw','118','5','Inten','5','1718.527663','0','0','598.0849757', '0.371338338','0.287112087']

我已经尝试了zip(行)和zip(行,val_list),但它们都没有。它是Row中数字的字符串格式的问题吗?如果是这样,我该如何解决这个问题呢?我也尝试了一些我在google上找到的方法,但是再一次,没有运气。

理想情况下,我会在执行声明中表达这一点,但在这方面的所有努力都是行不通的。

1 个答案:

答案 0 :(得分:0)

您使用VALUES枚举撤消了rows,请更改为以下内容:

with open(dataset) as f:
    ...
    # Don't change insert_string
    # insert_string += value_string + ')'
    for row in reader:
        # Append this row Data
        print 'Insert String = {}'.format(insert_string + row)
        sde_cursor.execute(insert_string + row)