Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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/61.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 pymysql占位符在(set)查询中使用WHILE列中断_Python_Mysql_String_Pymysql - Fatal编程技术网

Python pymysql占位符在(set)查询中使用WHILE列中断

Python pymysql占位符在(set)查询中使用WHILE列中断,python,mysql,string,pymysql,Python,Mysql,String,Pymysql,我正在使用pymysql使用占位符查询MySQL数据库。它工作得很好。我有一个查询引发了一个警告,因为在将参数加载到查询中时字符串的处理方式: 我的示例代码: thing_ids = "1, 2, 3, 4" list_notes = db_get_list("SELECT n.note_id, n.note_info, ... FROM note n, contact c WHERE n.note_created_by=c.contact_id AND n.note_on_thing_id I

我正在使用pymysql使用占位符查询MySQL数据库。它工作得很好。我有一个查询引发了一个警告,因为在将参数加载到查询中时字符串的处理方式:

我的示例代码:

thing_ids = "1, 2, 3, 4"
list_notes = db_get_list("SELECT n.note_id, n.note_info, ... FROM note n, contact c WHERE n.note_created_by=c.contact_id AND n.note_on_thing_id IN (%s)", (thing_ids, ))
而db_get_列表是:

def db_get_list(self, sql, args=None):
    with self.connection.cursor() as cursor:
        cursor.execute(sql, args)
        return cursor.fetchall()
提出的警告是:

/Library/Python/2.7/site-packages/pymysql/cursors.py:323: 
Warning: (1292, u"Truncated incorrect DOUBLE value: '1, 2, 3, 4'")
我在控制台上用MySQL做了一些调试。运行查询结束时:

WHERE .... AND n.note_on_thing_id IN ('1, 2, 3, 4')
WHERE .... AND n.note_on_thing_id IN (1, 2, 3, 4)
我得到了警告。运行查询结束时:

WHERE .... AND n.note_on_thing_id IN ('1, 2, 3, 4')
WHERE .... AND n.note_on_thing_id IN (1, 2, 3, 4)
(没有引用)没有警告

如何将参数传递给cursor.execute(),并在本例中要求它不要使用“引号”包装查询(每隔一次,我希望传递它时,与今天一样!)


我在文档中看到了其他%选项,例如%d,%g-但这些选项似乎只有在变量是整数类型而不是字符串类型时才起作用。该字符串将始终是一个包含逗号分隔的整数的字符串(它们表示列的主键ID)。

“该字符串将始终是一个包含逗号分隔的整数的字符串(它们表示列的主键ID)。”听起来就像是在表中的列中使用逗号分隔的值。。如果使用逗号分隔值,则应规范化数据,因为无法在逗号分隔值上选择IN运算符。。如果您无法更改使用MySQL的
FIND_IN_SET
函数所需的数据,则该函数旨在处理逗号分隔的值。谢谢!我已将查询更改为使用FIND_IN_SET而不是IN(),并且不再出错。