2

I am trying to get my Python code to count the number of records in my table. However, the Python shell keeps throwing up a "error in your SQL syntax" message. Does anyone have any ideas on what is wrong?

def count_rows(table):
 cur.execute(
 "SELECT COUNT(*) FROM %s",
 (table,)
 )
 cur.connection.commit()
count_rows("home_service")
Mureinik
316k54 gold badges403 silver badges406 bronze badges
asked Sep 29, 2015 at 16:05

1 Answer 1

1

Bound parameters, such as the execute function has can only be used to represent values - not object names or syntactic elements. If you want to dynamically determine set the table name, you'd have to resort to string manipulation:

cur.execute("SELECT COUNT(*) FROM %s" % table)
answered Sep 29, 2015 at 16:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.