Python-Warning:截断不正确的DOUBLE值

时间:2015-02-18 12:29:01

标签: python mysql mysql-python

我正在尝试使用mysql-python从“foo”表中获取“bar_ids”。为此,我收到以下错误,我只收到几个bar_ids。如何在没有警告错误的情况下正确获取此信息?

我的表格如下所示:

create table foo (foo_id int, bar_id int, foo_name varchar(255))

我的查询&错误:

foo_ids = 16600, 16602, 16607, 16609, 16611, 16613

警告:第1行的列'bar_id'的数据被截断

cursor.execute("""select bar_id from foo where foo_id in (%s)""", (foo_ids,))

警告:截断错误的DOUBLE值:'16600,16602,16607,16609,16611,16613'

cursor.execute("""select bar_id from foo where foo_id in (%s)""", (foo_ids,))

2 个答案:

答案 0 :(得分:3)

此问题之前已在SO中提出过。提出一个回应,这可能有助于提供一些背景和帮助@brisk继续阅读这种错误。

问题在于它将'foo_ids'作为字符串而不是单个值。所以它本质上是试图运行:

select bar_id from foo where foo_id in ('16600, 16602, 16607, 16609, 16611, 16613') 

注意报价?你希望它运行:

select bar_id from foo where foo_id in (16600, 16602, 16607, 16609, 16611, 16613) 

因此,当您报告错误时,它会在数字周围加上“引号”。

您可以考虑few solutions

答案 1 :(得分:1)

基本上,您需要自己重新创建参数列表:

param_list = '%s,' * (len(foo_ids) - 1) + '%s'
sql = """select bar_id from foo where foo_id in (%s)""" % param_list
cursor.execute(sql, foo_ids)