Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将变量传递到MySQLdb查询中?_Python_Mysql_Loops - Fatal编程技术网

如何使用Python将变量传递到MySQLdb查询中?

如何使用Python将变量传递到MySQLdb查询中?,python,mysql,loops,Python,Mysql,Loops,我试图循环一个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):

我试图循环一个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()
错误消息:

文件“james_test.py”,第114行,在“”,(i)中) 文件“/usr/lib64/python2.7/site packages/MySQLdb/cursors.py”,执行中第187行 query=query%元组([db.literal(item)用于args中的项]) TypeError:“int”对象不可编辑


cur.execute
参数需要
tuple
o
dict
,但您给出了
(i)
,它是
int
而不是
tuple
。要使其成为一个元组,请添加一个逗号,可以尝试以下操作:

a = '''this is the {try_}. try'''
i= 1    
b = a.format(try_=i)
print b
你甚至可以:

data = {'try_':i}
b = a.format(**data)
资料来源:


以下是我将如何做到这一点。您可能不需要声明2个游标,但这不会造成任何伤害。有时需要第二个光标,因为可能存在冲突。请注意,我如何演示循环光标数据的两种不同方法。一个是fetchall,另一个是循环光标。第三个方法可以使用fetch,但未显示。使用字典游标确实很好,但有时您可能希望使用标准的非dict游标,其中值只能通过行数组中的数字进行检索。还请注意,当只有1个参数时,需要在参数列表中使用尾随逗号。因为它需要一个元组。如果有多个参数,则不需要后面的逗号,因为多个parm将是一个元组

cursor1 = db.cursor(MySQLdb.cursors.DictCursor)  # a dictcursor enables a named hash
cursor2 = db.cursor(MySQLdb.cursors.DictCursor)  # a dictcursor enables a named hash

cursor1.execute("""
    Select user_id, solution_id 
      From user_concepts
     Where user_id IN (Select user_id FROM fields);
"""    

for row in cursor1.fetchall():
    user_id = row["user_id"]
    solution_id = row["solution_id"]

    cursor2.execute("""
        SELECT task_id, item_oid, influence
        FROM solution_oids 
        WHERE task_id = %s
        ORDER BY influence DESC;
    """, (solution_id,))

    for data in cursor2:
        task_id = data["task_id"]
        item_oid = data["item_oid"]
        influence = data["influence"]

这对我的帮助远不止我的问题。非常感谢。