简体   繁体   中英

SQLite3 passing a single placeholder value into a function in python - newbie

I am new in SQLite and I need help in this one. I am trying to make a simple function in python that takes a database name and table name and make it display the whole table. I wrote it like this:

def columnlist(dbname,tablename):
dbname=str(dbname)
tablename=str(tablename)
conn = sqlite3.connect(dbname) #connection
c = conn.cursor()
c.execute('SELECT sql FROM sqlite_master WHERE type=\'table\' AND name=?',tablename) #list columns in table 'students'
pp.pprint(c.fetchall())
conn.close()

Somehow, when I try to run it, it gave me the following error:

File "C:/Users/XXXX/Documents/Python Scripts/Assignment 3/L6Q1.py", line 20, in columnlist c.execute('SELECT sql FROM sqlite_master WHERE type=\\'table\\' AND name=(?)',str(tablename)) #list columns in table 'students'

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 8 supplied.

Can anyone please show me what I did wrong? Thanks

The problem is that you need to pass in a tuple instead of a single value. What you would want to do is change the line into the following:

c.execute('SELECT sql FROM sqlite_master WHERE type=\'table\' AND name=(?)',(tablename,))

Hope this helps :)

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