我有一个程序当前正在读取数据库,它将打印出当前数据库具有的表列表。
这是数据库链接:database(复制和粘贴)
我现在想要做的是获取用户输入以显示他们选择的特定表中的记录。我无法让用户选择显示记录。我正在使用SQLite3作为我的主要数据库软件。 我也很清楚这里的this question,但
使用嵌入在SQL上的
.format(category)
时,我总是收到错误消息。sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.
这是我到目前为止所做的:
import sqlite3
def get_data():
print("\nSelect a table: ", end="")
category = input()
category = str(category)
if '1' <= category <= '11':
print()
return category
else:
raise ValueError
def get_tables():
database = 'Northwind.db'
connection = sqlite3.connect(database)
c = connection.cursor()
sql = "SELECT * FROM sqlite_master WHERE type='table' AND NAME NOT LIKE 'sqlite_sequence' ORDER BY NAME "
x = c.execute(sql)
for row in x.fetchall():
table = row[1]
print(table)
def main():
category = get_data()
print(category)
get_tables()
if __name__ == "__main__":
main()
我希望这一切都有道理。感谢您的帮助。
复制注释:我的sql语句如下:
*)多行可读性sql = ("SELECT * FROM sqlite_master WHERE type='table' AND Name = ? AND NAME NOT LIKE 'sqlite_sequence'".format(category))
答案 0 :(得分:0)
您的SQL字符串应为:
sql = """SELECT * FROM sqlite_master
WHERE type='table'
AND Name = ?
AND NAME NOT LIKE 'sqlite_sequence'"""
,execute语句应为:
x = c.execute(sql, (category,))
还要确保您将category
作为参数传递给get_tables
函数。