簡體   English   中英

使用cx_python將列表插入到oracle db中會返回非法變量名稱/編號

[英]Insert List into oracle db with cx_python returns illegal variable name/number

使用保存在名為test.xml的本地文件上的此xml示例的內容,我正在嘗試解析內容並使用以下代碼插入到我的數據庫中:

import cx_Oracle
import re
import os
import xml.etree.ElementTree as ET
from ConfigParser import SafeConfigParser

def db_callmany(cfgFile, sql,params):

    parser = SafeConfigParser()
    parser.read(cfgFile)
    dsn = parser.get('odbc', 'dsn')
    uid = parser.get('odbc', 'user')
    pwd = parser.get('odbc', 'pass')
    try:
        con = None
        con = cx_Oracle.connect(uid , pwd, dsn)
        cur = con.cursor()
        cur.executemany(sql,params)
        con.commit()
    except cx_Oracle.DatabaseError, e:
            print 'Error %s' % e
            sys.exit(1)
    finally:
        if con:
            con.close()

if __name__ == '__main__':
    try:
        cfgFile='c:\\tests\\dbInfo.cfg'
        tree = ET.parse('test.xml')
        root = tree.getroot()
        mdfList = []
        for book in root.findall('book'):
            author = book.find('genre').text
            title = book.find('price').text
            the  = str((author,title))
            mdfList.append(the)

        sql = "INSERT INTO book_table ( GENRE, PRICE )"
        db_callmany(cfgFile,sql,mdfList)

    except KeyboardInterrupt:
        sys.stdout('\nInterrupted.\n')

但執行此代碼我收到以下錯誤:

Error ORA-01036: illegal variable name/number

Exit code:  1

不知道我可以在這里找到什么,以使此代碼工作。

CX_Oracle需要占位符,然后是executemany的一系列字典而不是序列序列 - 所以類似於:

mdfList = list()

for book in root.findall('book'):
    Values = dict()
    Values['GENRE'] = book.find('genre').text
    Values['PRICE'] = book.find('price').text
    mdfList.append(Values)

sql = "INSERT INTO book_table (GENRE, PRICE) VALUES (:GENRE, :PRICE)"
db_callmany(cfgFile, sql, mdfList)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM