简体   繁体   English

SQLite3将单个占位符值传递给python中的函数-Newbie

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

I am new in SQLite and I need help in this one. 我是SQLite的新手,我需要这一方面的帮助。 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. 我试图在python中创建一个简单的函数,该函数采用数据库名称和表名称并使其显示整个表。 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' 文件“ C:/ Users / XXXX / Documents / Python Scripts / Assignment 3 / L6Q1.py”,第20行,列列表c.execute(“从sqlite_master中选择SELECT sql,其中type = \\'table \\'AND name =(?) ',str(tablename))#表'students'中的列

ProgrammingError: Incorrect number of bindings supplied. ProgrammingError:提供的绑定数不正确。 The current statement uses 1, and there are 8 supplied. 当前语句使用1,并且提供了8。

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 :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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