代码之家  ›  专栏  ›  技术社区  ›  Krystian Cybulski

Python和psycopg2-原始sql使用整数条件从表中选择

  •  0
  • Krystian Cybulski  · 技术社区  · 17 年前

    这应该很简单,但我花了最后一个小时寻找答案。这是在Python2.6上使用psycopg2。

        special_id = 5
        sql = """
              select count(*) as ct,
                from some_table tbl
               where tbl.id = %(the_id)
          """
    cursor = connection.cursor()
    cursor.execute(sql, {"the_id" : special_id})
    

    我不能让它工作。是 special_id 一根绳子,我可以代替 %(the_id) 具有 %(the_id)s 事情进展顺利。但是,我希望它使用整数,以便它正确地命中我的索引。

    令人惊讶的是,互联网上缺少关于psycopg2的具体信息。我希望有人能回答这个看似简单的问题。

    1 回复  |  直到 12 年前
        1
  •  2
  •   Alex Martelli    17 年前

    每 PEP 249 paramstyle 是 pyformat ,您需要使用 %(the_id)s 即使对于非字符串,也要相信它会做正确的事情。

    h 但是,即使你拼错了,我也很惊讶你没有得到“你的意思是什么”的提示(我是在故意尝试的时候得到的!)。