【问题标题】:Inserting xls data into Oracle DB using cx_Oracle executemany使用 cx_Oracle executemany 将 xls 数据插入 Oracle DB
【发布时间】:2019-05-02 06:50:42
【问题描述】:

我正在尝试使用 cx_Oracle 将 xls 文件插入到 oracle 表中。以下是我尝试实现相同目标的方式。

wb = open_workbook('test.xls')
    values=[]
    sheets=wb.sheet_names()
xl_sheet=wb.sheet_by_name(s)
    sql_str=preparesql('MATTERS') ##this is function I have created which will return the insert statement I am using to load the table
    for row in range(1, xl_sheet.nrows):
        col_names = xl_sheet.row(0)
        col_value = []
        for name, col in zip(col_names, range(xl_sheet.ncols)):
            searchObj = re.search( (r"Open Date|Matter Status Date"), name.value, re.M|re.I)
            if searchObj:                           
                if (xl_sheet.cell(row,col).value) == '':
                    value = ''
                else:
                    value = datetime(*xlrd.xldate_as_tuple(xl_sheet.cell(row,col).value, wb.datemode))
                    value = value.strftime('%d-%b-%Y ')
            else:
                value = (xl_sheet.cell(row,col).value)
            col_value.append(value)
        values.append(col_value)

    cur.executemany(sql_str,values,batcherrors=True)

但是当我针对某些文件的多个 xls 文件对其进行测试时,它抛出 TypeError: I can't share the data due to the constraint from the client.我觉得这个问题与 excel 中比较列的 dtype 有关到数据库。有什么方法可以匹配上面值列表的 dtpes 以匹配 DB 中列的数据类型,还是有其他方法可以完成插入?我尝试使用 dataframe.to_sql 但它需要很多时间。我可以通过遍历值列表中的行来插入相同的数据。

【问题讨论】:

  • 可以分享一下错误信息吗?
  • 期待浮动

标签: python cx-oracle


【解决方案1】:

我建议您将数据导入 pandas 数据框,这样在 pandas 中使用数据类型变得非常容易。您可以更改整列的数据类型,并且可以轻松插入。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 2018-08-05
    • 1970-01-01
    • 2014-05-03
    • 2015-12-04
    • 2019-02-06
    • 2014-09-30
    • 2021-07-17
    • 2012-12-26
    相关资源
    最近更新 更多