简体   繁体   English

如何在 Python 中使用 GUI Tkinter 以特定格式显示来自数据库 sqlite3 的信息?

[英]How to display information from database sqlite3 using GUI Tkinter in Python in a specific format?

I am trying to retrieve information from the database and display it in this certain way:我正在尝试从数据库中检索信息并以某种方式显示它:

Name: Batman名称:蝙蝠侠

Price: £ 18价格:18 英镑

But I am getting this error: "TypeError: 'int' object is not subscriptable."但我收到此错误:“TypeError:'int' object 不可下标。”

How can I fix that?我该如何解决?

c.execute("SELECT name, price FROM products WHERE id=02")
    records = c.fetchone()
    # print(records)

    print_products = ''
    for record in records:
        print_products += 'Name:' + str(record[0]) + '\n' + 'Price: £' + str(record[1])

    query_label = Label(frame, fg='darkblue', bg='darkgray', text=print_products)
    query_label.pack()

Since you only fetch one record from the result, records is a tuple of column values.由于您只从结果中获取一条记录,因此records是列值的元组。 Then you use for record in records: , it means record is a column value that may be an integer.然后你使用for record in records: ,这意味着record是一个列值,可能是 integer。 So using record[0] will raise the mentioned exception.所以使用record[0]会引发上述异常。

Actually you don't need the for loop at all:实际上你根本不需要 for 循环:

c.execute("SELECT name, price FROM products WHERE id=02")
record = c.fetchone()

# check whether record exists
if record:
    print_products = 'Name:' + str(record[0]) + '\n' + 'Price: £' + str(record[1])

    query_label = Label(frame, fg='darkblue', bg='darkgray', text=print_products)
    query_label.pack()

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

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