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 ;)
In the psycopg2 adapter library you can return the code by accessing the exception's pgcode attribute. It should be an alpha-numeric string, five characters in length, that corresponds to an exception in the PostgreSQL Error Codes table.
Thread and process safetyThe Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors. In DB API 2.0 parlance, Psycopg is level 2 thread safe.
Prerequisites. The current psycopg2 implementation supports: Python versions from 3.6 to 3.11. PostgreSQL server versions from 7.4 to 15.
Psycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With