我想循環MySQL查詢,但我無法讓變量工作。我究竟做錯了什麼?循環在行開始10如何將變量傳遞到Python的MySQLdb查詢?
cur = db.cursor()
query = '''
Select user_id, solution_id
From user_concepts
Where user_id IN
(Select user_id FROM fields);
'''
cur.execute(query)
numrows = cur.rowcount
for i in xrange(0,numrows):
row = cur.fetchone()
# find all item_oid where task_id = solution_id for first gallery and sort by influence.
cur.execute('''
SELECT task_id, item_oid, influence
FROM solution_oids
WHERE task_id = row[%d]
ORDER BY influence DESC;
''', (i))
cur.fetchall()
錯誤消息:
File "james_test.py", line 114, in ''', (i)) File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute query = query % tuple([db.literal(item) for item in args]) TypeError: 'int' object is not iterable
這幫助我比我的問題更多。非常感謝。 –