2

我正在尝试解析多个 CSV 并使用 cx_Oracle 将它们的数据插入到表中。我使用execute插入表没有问题,但是当我尝试使用executemany执行相同的过程时出现错误。我使用执行的代码是

with open(key,'r') as file:
    for line in file:
        data = data.split(",")
        query = "INSERT INTO " + tables[key] + " VALUES ("
        for col in range(len(data)):
            query += ":" + str(col) + ","
        query = query[:-1] + ")"            
        cursor.execute(query, data)

但是当我用

with open(key,'r') as file:
    list = []
    for line in file:
        data = data.split(",")
        list.append(data)
    if len(list) > 0:
        query = "INSERT INTO " + tables[key] + " VALUES ("
        for col in range(len(data)):
            query += ":" + str(col) + ","
        query = query[:-1] + ")"            
        cursor.prepare(query)
        cursor.executemany(None,list)

尝试插入具有 CLOB 列且数据超过 4000 字节的表时,出现“ValueError:字符串数据太大”。当表没有 CLOB 列时,Executemany 效果很好。有没有办法告诉 cx_Oracle 在执行时将适当的列视为 CLOB?

4

1 回答 1

4

尝试将大列的输入大小设置为cx_Oracle.CLOB. 如果您有二进制数据可能不起作用,但应该适用于您在CSV. 该2K值可能低于它需要的值。

请注意,executemany当涉及列时,这似乎要慢很多CLOB,但仍然比重复执行要好:

def _executemany(cursor, sql, data):
    '''
    run the parameterized sql with the given dataset using cursor.executemany 
    if any column contains string values longer than 2k, use CLOBS to avoid "string
    too large" errors.

    @param sql parameterized sql, with parameters named according to the field names in data
    @param data array of dicts, one per row to execute.  each dict must have fields corresponding
                to the parameter names in sql
    '''
    input_sizes = {}
    for row in data:
        for k, v in row.items():
            if isinstance(v, basestring) and len(v) > 2000:
                input_sizes[k] = cx_Oracle.CLOB
    cursor.setinputsizes(**input_sizes)
    cursor.executemany(sql, data)
于 2012-08-28T05:36:48.237 回答