繁体   English   中英

在 SQLite3 Python 中选择与用户输入匹配的 ID

[英]Selecting an ID which matches a user input in SQLite3 Python

我想给用户一个选项来选择一个 ID 来查看 matplotlib 报告......但是,我坚持选择类别与用户输入匹配的数据(id_input)

if report_option == "A":
    print("Expense Report by Category: \n")
    id_input = input("Please enter the ID of the Category you would like to view: \n")
    c.execute("SELECT * FROM tblCategory")
    for row in c.fetchall():
        print(row)
    c.execute('SELECT CategoryID, AmountSpent FROM tblFinance')
    categories = []
    amountspent = []
    for row in c.fetchall():
        print(row[0])
        categories.append(row[0])
        amountspent.append(row[1])

    plt.plot(categories, amountspent)
    plt.ylabel('Amount Spent')
    plt.xlabel('Category ID')
    plt.show()
    menu()

任何帮助将不胜感激!

我假设您尝试从tblFinance中选择AmountSpent ,其中CategoryID是用户输入的内容。

id_input = input("Please enter the ID of the Category you would like to view: \n")
data = []
for row in c.execute("SELECT AmountSpent FROM tblFinance WHERE CategoryID=?", (id_input, )):
   data.append(row)
return data #use data variable here however you need with matplotlib

解释:

您要创建一个 SELECT x FROM y WHERE z = a 语句,该语句从表 y 中选择 x,其中 z 是 a。 基本上就是它所说的。 你可以用? 以及根据输入获取数据的第二个参数。 如果您有更多问题,请告诉我。

编辑:

如果您想同时选择 AmountSpent 和 CategoryID,请将 SQL 语句替换为"SELECT CategoryID, AmountSpent FROM tblFinance WHERE CategoryID=?"

暂无
暂无

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

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