简体   繁体   中英

Python: Error in SQL Syntax

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM