我是 Python 新手,我想通过写入 DB2 数据库来尝试一下。Python 是 2.7,在 iSeries 上运行。对于我的待办事项清单上的各种事情,我喜欢 Python 而不是 RPG 或 CL 的想法,所以我从第一原则开始......但是我遇到了一个问题 - 我似乎不明白 Python 是如何处理字符串。这是一个例子:
请解释原因:
db2cursor.execute('insert into trickledb.trickledb values(?, ?, ?, ?, ?, ?, ?, ?)',
('01100158059642','1055','2012-09-12','13:30:05','0700','1','1','28010'))
完全没问题,(这些值被插入到我的数据库中)但是:
sql="'insert into trickledb.trickledb values(?, ?, ?, ?, ?, ?, ?, ?)',
('01100158059642','1055','2012-09-12','13:30:05','0700','1','1','28010')"
db2cursor.execute(sql)
给出错误:
db2cursor.execute(sql)
db2.Error: SQLState: 42601, Error code: -104
Token 'insert into trickledb.trickle was not valid.
Valid tokens: ( END GET SET CALL DROP FREE HOLD.
我的脑袋疼!
这是我的完整脚本 - 我正在从 XML 文件中提取数据:
#!/usr/bin/env python
# (Be in -*- python -*- mode.)
import db2
from xml.dom import minidom
db2connection = db2.connect()
db2cursor = db2connection.cursor()
xmldoc = minidom.parse('test.XML')
itemlist = xmldoc.getElementsByTagName('PluSale')
for s in itemlist :
print s.attributes['PluCode'].value, s.attributes['TicketNumber'].value, s.attributes['Time'].value, s.attributes['Date'].value, s.attributes['StoreNumber'].value, s.attributes['PosNo'].value, s.attributes['Qty'].value, s.attributes['SequenceNbr'].value
PluCode = s.attributes['PluCode'].value
TicketNumber = s.attributes['TicketNumber'].value
Time = s.attributes['Time'].value
Date = s.attributes['Date'].value
StoreNumber = s.attributes['StoreNumber'].value
PosNo = s.attributes['PosNo'].value
Qty = s.attributes['Qty'].value
SequenceNbr = s.attributes['SequenceNbr'].value
string = "%s,%s,%s,%s,%s,%s,%s,%s" %(PluCode,TicketNumber,Date,Time,StoreNumber,PosNo,Qty,SequenceNbr)
list = [PluCode,TicketNumber,Date,Time,StoreNumber,PosNo,Qty,SequenceNbr]
tuple = (PluCode,TicketNumber,Date,Time,StoreNumber,PosNo,Qty,SequenceNbr)
print string
print list
print tuple
db2cursor.execute ('insert into trickledb.trickledb values(?, ?, ?, ?, ?, ?, ?, ?)', tuple)
db2connection.commit()
db2cursor.close()
db2connection.close()
我一直在 .execute 语句参数位中用带有列表的字符串替换元组,没有任何喜悦。我确定这很简单,我不完全理解。这是我真正想克服的绊脚石,因为我喜欢在 iSeries 上使用 Python 的想法……与 RPG 或 CL 相比,它太强大了……