我在下面的代碼中找不到我的錯誤。當它運行一個類型給出誤差線:cur.executemany(SQL%itr.next())>'函數採用恰好2個參數(1給出),SQLite executemany問題
import sqlite3
con = sqlite3.connect('test.sqlite')
cur = con.cursor()
cur.execute("create table IF NOT EXISTS fred (dat)")
def newSave(className, fields, objData):
sets = []
itr = iter(objData)
if len(fields) == 1:
sets.append(':' + fields[0])
else:
for name in fields:
sets.append(':' + name)
if len(sets)== 1:
colNames = sets[0]
else:
colNames = ', '.join(sets)
sql = " '''insert into %s (%s) values(%%s)'''," % (className, colNames)
print itr.next()
cur.executemany(sql % itr.next())
con.commit()
if __name__=='__main__':
newSave('fred', ['dat'], [{'dat':1}, {'dat':2}, { 'dat':3}, {'dat':4}])
我感謝你的想法。
看到這個稱號讓我瘋狂。標題應該總結實際的問題。 – 2009-06-23 06:40:32