gpt4 book ai didi

python - 如何使用 Python 将变量传递给 MySQLdb 查询?

转载 作者:行者123 更新时间:2023-11-29 01:53:56 24 4
gpt4 key购买 nike

我正在尝试遍历 MySQL 查询,但是我无法让变量工作。我究竟做错了什么?循环从第 10 行开始。

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

最佳答案

cur.execute 期望一个 tuple o dict 参数但是你给了 (i) 这是一个int 不是 tuple。要使其成为元组,请添加逗号(i,)

关于python - 如何使用 Python 将变量传递给 MySQLdb 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34218020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com