Psycopg2 使用通配符会导致 TypeError

发布于 2024-09-27 15:11:02 字数 616 浏览 6 评论 0原文

目前我正在尝试搜索数据库以获取某些事件。我的查询就是这样

SELECT * FROM events WHERE summary ILIKE E'%test%' AND start_time > '2010-10-01'

简单地说,我需要查询来浏览日历事件的数据库,并返回任何带有“测试”摘要的内容以及本月初之后的内容。

从数据库命令行查询时,这将返回预期结果。然而,当我尝试在带有 psycopg2 的 Python 脚本中使用它时:

cursor.execute("SELECT * FROM events WHERE summary ILIKE E'%test%' AND start_time > %(begin)s ", {'begin' : datetime.datetime(2010,10,1) })

我收到类型错误

*** TypeError: 'dict' object does not support indexing

进行一些初始谷歌搜索,这听起来像是我使用通配符的方式。但我可能是错的,我可能错过了一些我看不到的简单的东西。希望社区的新目光能够纠正我的愚昧;)

Currently I am attempting to search a database to grab certain events. My query is as such

SELECT * FROM events WHERE summary ILIKE E'%test%' AND start_time > '2010-10-01'

Simply put I need the query to look through a database of calendar events and return anything with a summary with 'test' in it and after the beginning of this month.

This returns the expected results when queried from the database command line. However when I attempt to use it in my Python script with psycopg2 as such:

cursor.execute("SELECT * FROM events WHERE summary ILIKE E'%test%' AND start_time > %(begin)s ", {'begin' : datetime.datetime(2010,10,1) })

I get a type error

*** TypeError: 'dict' object does not support indexing

Doing some initial Googling it sounds like something with the way I'm using my wildcards. I could be wrong though and I am probably missing something simple that I don't see. Hopefully a fresh pair of eyes from the community can correct my noobishness ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-10-04 15:11:02

不确定这是否是问题的全部根源,但我认为您需要转义通配符,否则参数化逻辑会感到困惑。

SELECT * FROM events WHERE summary ILIKE E'%%test%%' AND start_time > %(begin)s 

编辑:10多年后,我正在查看 psycopg2 问题是否有一个不相关的错误,发现开发人员

Not sure if this is the full root of your problem, but I think you need to escape your wildcards or the parameterization logic will get confused.

SELECT * FROM events WHERE summary ILIKE E'%%test%%' AND start_time > %(begin)s 

EDIT: 10+ years later, I'm looking at the psycopg2 issues for an unrelated bug and found that the developers recognize this as an issue. My comments below were correct. If you're passing arguments in your query, then you must escape the percent signs in your LIKE clause. If you're not passing arguments, then you don't escape your LIKE clause.

殤城〤 2024-10-04 15:11:02

我的猜测是你的“%”让Python感到困惑。在 psycopg2 中,我做我的通配符“like”查询,如下所示:


#!/usr/bin/python

import sys,os.path,psycopg2
db=psycopg2.connect("dbname=music")

for line in sys.argv[1::]:
    cursor=db.cursor()
    key="%"+line+"%"
    cursor.execute("select count(*) from pool where path like %s",(key,))
    if cursor.fetchone()[0] != 1:
        sys.stderr.write("ambiguous stem or no such song")
        sys.exit(-1)
    cursor.execute("insert into spool select path from pool where path like %s",(key,))
    cursor.close()
    db.commit()
db.close()

使用用户提供的搜索字符串(如在此脚本中),您可能希望转义其中的任何“%”,我怀疑这将是查询中的合法通配符,但我还没有做到这一点

my guess is something about your "%"'s is confusing python. in psycopg2 i do my wildcard "like" queries like this:


#!/usr/bin/python

import sys,os.path,psycopg2
db=psycopg2.connect("dbname=music")

for line in sys.argv[1::]:
    cursor=db.cursor()
    key="%"+line+"%"
    cursor.execute("select count(*) from pool where path like %s",(key,))
    if cursor.fetchone()[0] != 1:
        sys.stderr.write("ambiguous stem or no such song")
        sys.exit(-1)
    cursor.execute("insert into spool select path from pool where path like %s",(key,))
    cursor.close()
    db.commit()
db.close()

with user-supplied search strings like in this script you would probably want to escape out any "%"'s in them, which i suspect would otherwise be legit wildcards in the query, but i haven't gotten that far yet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文