0
我在第一次迭代中使用此代碼獲得TypeError: cannot serialize ('John',) (type tuple)
。爲什麼SELECT name FROM potluck
輸出'John',
而不是'John'
?TypeError:無法序列化 - 類型元組
('John',)
('Sandy',)
('Tom',)
('Tina',)
無論如何,我不知道問題出在那。
#!/usr/bin/env python
import psycopg2
import sys
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
con = None
try:
con = psycopg2.connect(database='events', user='demo')
cur = con.cursor()
cur.execute("SELECT name FROM potluck")
rows = cur.fetchall()
top = Element('top')
for row in rows:
#print row
comment = Comment('Generated test')
top.append(comment)
child = SubElement(top, 'child')
child.text = row
print tostring(top)
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()
您會在哪一行發生錯誤? –