python oracle插入数据类型不匹配 [英] python oracle insert datatype mismatch
本文介绍了python oracle插入数据类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在[19]中:selrow
输出[19]:[{'CUSTOMER_NM':'ABC INC'
'CUSTOMER_NO':'A0050129',
'CUSTOMER_SK':926,
'EFFECTIVE_DT':datetime.datetime(2015,10,2,0,0)}]
这是我的插入语句:
code>在[30]中:cur = db.cursor()
cur.execute('INSERT INTO DIM_CUST(%s)VALUES(%s)',(selrow [0] .keys() selrow [0] .values()))
db.commit()
和我我收到以下错误:
-------------------- -------------------------------------------------- -----
TypeError Traceback(最近的最后一次调用)
< ipython-input-30-eefac6fb0aa7>在< module>()
2 #query ='INSERT INTO DIM_CUST(%s)VALUES(%s)',(selrow [0] .keys(),selrow [0] .values())
3#打印查询
----> 4 cur.execute('INSERT INTO DIM_CUST(%s)VALUES(%s)',(selrow [0] .keys(),selrow [0] .values()))
5 db.commit()
TypeError:期望数字数据
我的插入语法是否正确?我使用字典键作为列名和字典值作为要输入到表中的值。
pl。问我可能已经省略的任何细节。感谢您提供的任何帮助。
解决方案
谢谢Lucas& haraprasadj。根据你的指针,我想出了这一点:
cols =','.join(selrow [0] .keys )
vals =':'+',:'。join(selrow [0] .keys())
sql ='INSERT INTO DIM_CUST(%s)VALUES(%s)'% cols,vals)
cur.execute(sql,selrow [0])
db.commit()
它的作品!支持你的帮助。
I am trying to insert a dictionary into an Oracle table and its failing with data mismatch error. I want to know if the syntax I am using is a correct one.
In [19]:selrow
Out[19]:[{'CUSTOMER_NM': 'ABC INC',
'CUSTOMER_NO': 'A0050129',
'CUSTOMER_SK': 926,
'EFFECTIVE_DT': datetime.datetime(2015, 10, 2, 0, 0)}]
Here is my insert statement:
In [30]:cur=db.cursor()
cur.execute('INSERT INTO DIM_CUST (%s) VALUES (%s)', (selrow[0].keys(), selrow[0].values()))
db.commit()
and I am getting the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-eefac6fb0aa7> in <module>()
2 #query = 'INSERT INTO DIM_CUST (%s) VALUES (%s)', (selrow[0].keys(), selrow[0].values())
3 #rint query
----> 4 cur.execute('INSERT INTO DIM_CUST (%s) VALUES (%s)', (selrow[0].keys(), selrow[0].values()))
5 db.commit()
TypeError: expecting numeric data
Is my insert syntax correct? I am using dictionary keys as column names and dictionary values as the values to be entered into the table.
pl. ask any details i may have omitted. Thank you for any help that you can provide.
解决方案
Thank you Lucas & haraprasadj. Based on your pointers, I came up with this:
cols = ', '.join(selrow[0].keys())
vals = ':'+', :'.join(selrow[0].keys())
sql='INSERT INTO DIM_CUST (%s) VALUES (%s)' % (cols, vals)
cur.execute(sql, selrow[0])
db.commit()
It works! appeciate your help.
这篇关于python oracle插入数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文