psycopg2 “IndexError: tuple index out of range” Error when using '%' like operator with arguments tuple

This works fine:

 cc.execute("select * from books where name like '%oo%'")

But if second argument passed:

cursor.execute("select * from books where name like '%oo%' OFFSET % LIMIT %", (0,1))

Psycopg errors:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

How to avoid this error?


First of all, you should use %% to insert % literal, otherwise, library will try to use all % as placeholders. Second, it's better to specify %s, where you want to insert values.

So, your code should look like:

cursor.execute("select * from books where name like '%%oo%%' OFFSET %s LIMIT %s", (0,1))

Need Your Help

How use python on ipad?

python ipad

Recently, I've found that the ipad can run python with a special python interpret. But editing the code on ipad is a terrible nightmare. So how can I push the python code which has been edited comp...

code for when you run a Data Driven

java selenium-webdriver apache-poi

When performing data driven in a given field , the code for its implementation , not running the next step of the code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.