Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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/8/mysql/67.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_Mysql Python - Fatal编程技术网

如何防止从Python向MySQLdb语句插入额外的引用?

如何防止从Python向MySQLdb语句插入额外的引用?,python,mysql,mysql-python,Python,Mysql,Mysql Python,我有一系列SQL select语句,需要使用MySQLdb从Python脚本运行这些语句。我想传递给select语句的变量之一称为unit_ID。我试着将单元ID视为字符串以及字符串的元组。最初,反斜杠被插入到字符串中。在网上浏览之后,我已经能够避免反斜杠,但是现在额外的引号被插入了。这是我目前的代码: connection = MySQLdb.connect('localhost', 'root', '*****', 'test') cur = connection.cursor unit_

我有一系列SQL select语句,需要使用MySQLdb从Python脚本运行这些语句。我想传递给select语句的变量之一称为unit_ID。我试着将单元ID视为字符串以及字符串的元组。最初,反斜杠被插入到字符串中。在网上浏览之后,我已经能够避免反斜杠,但是现在额外的引号被插入了。这是我目前的代码:

connection = MySQLdb.connect('localhost', 'root', '*****', 'test')
cur = connection.cursor

unit_ids = ('0A1', '0A2', '0A3', '0A4')
attr = 'sample'

cur.execute("""SELECT COUNT(*) FROM test WHERE attribute = %s AND unit_id IN %r""", (a, tuple(unit_ids)))
使用cur._last_executed,我可以看到实际执行的SQL语句是:

SELECT COUNT(*) FROM test WHERE attribute = 'sample' AND unit_id IN ("'0A1'", "'0A2'", "'0A3'", "'0A4'")
为了使SQL语句中的“0A1”、“0A2”、“0A3”、“0A4”保持不变,我需要更改哪些内容,您有什么想法吗

更新:以下是我在使用%s时得到的确切输出:

此时,我想我可能只需要为unit_id的每个元素创建单独的变量,例如unit_id1='0A1'。顺便说一下,我使用的是Python2.7.9和MySQL服务器5.6


更新2:@thebjorn解决了这个问题:我的MySQLdb版本已经过时。升级后,SQL语句中不再插入额外的引号。

您不需要任何魔法,只需按常规mysql方式执行即可:

connection = MySQLdb.connect('localhost', 'root', '*****', 'test')
cur = connection.cursor()

unit_ids = ('0A1', '0A2', '0A3', '0A4')
attr = 'sample'

cur.execute("""SELECT COUNT(*) FROM test WHERE attribute = %s AND unit_id IN %s""", (a, unit_ids))
我能看到的唯一问题是,如果没有包含a,而unit_id只有一个项,那么元组语法可能会让您出错。如果将unit_ID放入一个列表中,那么语法就不会那么麻烦了:

unit_ids = ('0A1',)
cur.execute("SELECT COUNT(*) FROM test WHERE unit_id IN %s", (unit_ids,))
当内联成为:

cur.execute("SELECT COUNT(*) FROM test WHERE unit_id IN %s", (('0A1',),))
与使用列出一个参数相比,该参数是一个项目列表:

cur.execute("SELECT COUNT(*) FROM test WHERE unit_id IN %s", [['0A1']])
您可以对所有mysql参数使用%s,也可以使用其他一些参数,但不能使用%r-这不是字符串插值

更新:你一定和我做了一些不同的事情。。这是游标的输出。上次执行

>>> cn = MySQLdb.connect('server', 'user', 'password', 'database')
>>> c = cn.cursor()
>>> unit_ids = ('0A1', '0A2', '0A3', '0A4')
>>> c.execute("select count(*) from foo where id in %s", (unit_ids,))
Traceback (most recent call last):
  File ...
_mysql_exceptions.ProgrammingError: (1146, "Table 'djangodevelop.foo' doesn't exist")
>>> c._last_executed
"select count(*) from foo where id in ('0A1','0A2','0A3','0A4')"
>>>
不要在%r中的单元id中使用%r。Python Mysql数据库API在生成SQL查询时仅支持%s作为占位符

在前面的示例中,我们将SELECT语句存储在变量查询中。请注意,我们使用的是未加引号的%s标记,其中日期应为。Connector/Python将hire_start和hire_end从Python类型转换为MySQL能够理解的数据类型,并添加所需的引号。在本例中,它将第一个%s替换为“1999-01-01”,第二个替换为“1999-12-31”

您可以在中看到类似的警告和用法


也许可以试试:unit_id='0A1,0A2,0A3,0A4'这很有趣。printSELECT COUNT*来自测试,其中属性=%s和%r,a,tupleunit\u id中的单位id对我有效,和cur.execute应该符合相同的语法。或者,如果我在第一个代码块中运行代码,然后添加行printcur,则在execute字符串中添加尾随逗号,并仅使用单位id,而不是tupleunit id。如果我在第一个代码块中运行代码,然后添加行printcur,则最后一次执行显示发送到数据库的确切SQL语句,我仍然在“0A”、“0A2”、“0A3”、“0A4”中看到…和单位id。代码是有效的,因为它不会抛出错误,但是unit_ID中每个元素周围的附加引号会导致select语句不正确。我不会得到额外的引号,请参阅更新的答案。您是否记得将%r更改为%s?@zkstine您使用了错误的参数%r。在生成查询时不要使用该选项。检查我的答案。也许你需要升级。。?我现在是1.3分。3@thebjorn就这样!升级MySQLdb后,不再有额外的报价。谢谢感谢您抽出时间回答@FallenAngel@Bjorn已经指出我需要使用%s而不是%r。现在我只想将整个变量unit_id传递给SQL语句,而不是显式地传递给每个元素,但是当我这样做时,会在每个元素周围插入额外的引号。
>>> cn = MySQLdb.connect('server', 'user', 'password', 'database')
>>> c = cn.cursor()
>>> unit_ids = ('0A1', '0A2', '0A3', '0A4')
>>> c.execute("select count(*) from foo where id in %s", (unit_ids,))
Traceback (most recent call last):
  File ...
_mysql_exceptions.ProgrammingError: (1146, "Table 'djangodevelop.foo' doesn't exist")
>>> c._last_executed
"select count(*) from foo where id in ('0A1','0A2','0A3','0A4')"
>>>
cur.execute("""
    SELECT COUNT(*) FROM test WHERE attribute = %s AND unit_id IN %s
""", (a, ('0A1', '0A2', '0A3', '0A4')))