Psycopg2 使用通配符导致 TypeError [英] Psycopg2 using wildcard causes TypeError

查看:46
本文介绍了Psycopg2 使用通配符导致 TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我收到类型错误

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

做一些初步的谷歌搜索,听起来像是我使用通配符的方式.不过,我可能是错的,我可能遗漏了一些我看不到的简单内容.希望来自社区的一双新鲜的眼睛可以纠正我的noobishness ;)

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

推荐答案

不确定这是否是您问题的全部根源,但我认为您需要转义通配符,否则参数化逻辑会变得混乱.

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 

我认为 %% 是正确的转义,但它可能是 \%

I think %% is the correct escaping, but it could be \%

这篇关于Psycopg2 使用通配符导致 TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆