I ran into another silly error, for which I wanted to share the solution in order to save you some time. It occurs when trying to run a query using Python’s SQLAlchemy libary. Let’s dive right in.
The problem
When you’re trying to run a query, either by using Pandas’ read_sql or SQLAlchemy’s execute, there’s a chance you’ll run into this cryptic error message:
TypeError: ‘dict’ object does not support indexing
Initially, I had no idea what was going on. However, my query contained percentage signs (%), in a LIKE operator. And percentage signs are special in Python.
q = "SELECT * FROM sometable WHERE category LIKE '%somestring%'" engine.execute(q) # Using SQLAlchemy: doesn't work pd.read_sql(q) # Using Pandas: doesn't work either, because Pandas uses SQLAlchemy
The problem here is that the percentage sign is treated as bind parameter. SQLAlchemy expects the percentage to be a parameter of type ‘format’ or ‘pyformat’.
Note above, the usage of a question mark “?” or other symbol is contingent upon the “paramstyle” accepted by the DBAPI in use, which may be any of “qmark”, “named”, “pyformat”, “format”, “numeric”. See pep-249 for details on paramstyle.
SQLAlchemy documentation
The solution
There are two sensible ways to make SQLAlchemy ignore the bind parameters.
First, selectively escape the percentage sign. Intuitively you’d do this with a backslash. However, this doesn’t work: you need to specify a second %.
q = "SELECT * FROM sometable WHERE category LIKE '%%somestring%%'" engine.execute(q)
A second solution is to use SQLAlchemy’s text function. It’s main use is constructing a query with bind parameters, but if none are specified, it explicitly ignores the percentages signs.
q = sqlalchemy.text("SELECT * FROM sometable WHERE category LIKE '%somestring%'") engine.execute(q)
Great success!
Do you have a spam problem on this website; I also am a blogger,
and I was wanting to know your situation; many of us have created some nice procedures and we are looking to
exchange strategies with other folks, please shoot me an e-mail if interested.
Bel article, je l’ai partagé avec mes amis.
Bel article, je l’ai partagé avec mes amis.
Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot
you an e-mail. I’ve got some suggestions for your blog you might be
interested in hearing. Either way, great website and I look forward to
seeing it improve over time.
Howdy! Do you know if they make any plugins to help with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m
not seeing very good gains. If you know of any please share.
Thanks!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.info/ru/join?ref=JHQQKNKN
Your article helped me a lot, is there any more related content? Thanks! https://www.binance.com/id/join?ref=DB40ITMB
buysteriodsonline.com
그러므로 치료를 받고자 한다면 조급해 하지 말고 이런 방향으로 공부를 해야 합니다.