【问题标题】:Python-Warning: Truncated incorrect DOUBLE valuePython-警告:截断不正确的 DOUBLE 值
【发布时间】:2015-02-18 12:29:01
【问题描述】:

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

我的表格如下图:

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,))

【问题讨论】:

标签: python mysql mysql-python


【解决方案1】:

这个问题之前已经在 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) 

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

有一个fewsolutions可以考虑。

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多