Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.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
PyMysql-Python:如何将两个输入与一个sql查询关联起来?_Python_Mysql_Pymysql - Fatal编程技术网

PyMysql-Python:如何将两个输入与一个sql查询关联起来?

PyMysql-Python:如何将两个输入与一个sql查询关联起来?,python,mysql,pymysql,Python,Mysql,Pymysql,就我的问题来说,我们有这个表: Actor1 Actor2 Movies stef bill a bill stef b nick nick c stef stef d nick bill e bill nick f 我们想找到演电影的演员,我通过键盘输入演员的名字(input1,input2)。 显然,如果输入是一个参与者,我们会有这样的结果: sql_query='select Actor1,Actor2 from movies where

就我的问题来说,我们有这个表:

Actor1 Actor2 Movies
stef   bill   a
bill   stef   b
nick   nick   c
stef   stef   d
nick   bill   e
bill   nick   f
我们想找到演电影的演员,我通过键盘输入演员的名字(input1,input2)。 显然,如果输入是一个参与者,我们会有这样的结果:

sql_query='select Actor1,Actor2 from movies where Actor1= {}'.format(input1)
cursor.execute(sql_query)

但是当我有两个输入时,我应该怎么做?

在查询中使用两个占位符。您还应该让
cursor.execute()
执行占位符替换,因为它使用的是一条准备好的语句,而不是字符串格式,这对SQL注入是开放的(您还忘记了
{}
周围的引号,因此可能会出错)

sql_query = 'select Actor1,Actor2 from movies where (Actor1= %s AND Actor2 = %s) OR (Actor1 = %s AND Actor2 = %s)'
cursor.execute(sql_query, (input1, input2, input2, input1))