使用Python

时间:2017-01-24 15:09:59

标签: python oracle oracle-sqldeveloper

我知道这是一个非常简单的问题,但我很难过。当我使用Python通过SQLDeveloper将一大块数据插入到我的Oracle表中时,我很成功,但是当我还尝试插入一些额外的值时,它失败了。

我有一个23行5列的DataFrame,我可以轻松地将它导出到我的Oracle表中。我还希望用一个值填充表格的一列," NEFS 2",另一列用另一个值," 1",我不能成功执行这两个导出。

这是我的导出代码:

cursor = con.cursor()
exported_data = [tuple(x) for x in df_Quota.values]
sql_query = (("INSERT INTO ROUGHTABLE(species, date_posted, stock_id, pounds, money)" "VALUES (:1, :2, :3, :4, :5)"), ("INSERT INTO ROUGHTABLE(sector_name, ask)" "VALUES (NEFS 2, 1)"))
cursor.executemany(sql_query, exported_data)
con.commit()

cursor.close()
con.close()

它在cursor.executemany(sql_query, exported_data)行上失败,错误为TypeError: expecting string or bytes object

我是否需要将sql_query分成两个不同的陈述?如果是,那么我是否需要有两个不同的cursor.executemany语句?或者这是一个比这更大的问题?请原谅我是SQL的新手。并感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

据我了解,您希望用值(:1,:2,:3,:4,:5,&#39)填充ROUGHTABLE(species,date_posted,stock_id,磅,money,sector_name,ask) ; NEFS 2',1)。如果是这种情况,则需要的SQL是:

INSERT INTO ROUGHTABLE(species, date_posted, stock_id, pounds, money, sector_name, ask) 
VALUES (:1, :2, :3, :4, :5, 'NEFS 2', 1)

python代码的第3行应该是:

sql_query = "INSERT INTO ROUGHTABLE (species, date_posted, stock_id, pounds, money, sector_name, ask) VALUES (:1, :2, :3, :4, :5, 'NEFS 2', 1)"