sqlite “索引错误:列表索引超出范围”检索图像和文本

eblbsuwk  于 2022-11-24  发布在  SQLite
关注(0)|答案(1)|浏览(112)

当使用Python中的tkinter从sqlite3数据库中检索图像和文本时,即使数据库中有3个对象(2个文本和1个blob),我也会收到错误。
编码:

def retrieve():
    global  ntr, img, mylabel

    conn = sqlite3.connect('info.db')
    c = conn.cursor()
    c.execute("SELECT * FROM students WHERE id_name like ? ",(ntr.get(),))
    records = c.fetchmany()
    
    first1 = records[0]
    second2 = records[1]
    third = records[2]

    img = Image.open(io.BytesIO(third))
    img = img.resize((80, 80))
    img =ImageTk.PhotoImage(img)
    mylabel = Label(root, image=img, width=80, height=80)
    mylabel.grid()

    mylabel2 = Label(root, text =first1)
    mylabel2.grid()

    mylabel3 =Label(root, text= second2)
    mylabel3.grid()

错误:
second2 =记录[1]索引错误:列表索引超出范围

cnh2zyt3

cnh2zyt31#

如果您尝试执行通配符搜索,则需要将通配符放在单引号中:

"SELECT * FROM students WHERE id_name like ? "

变成

"SELECT * FROM students WHERE id_name like '?' "

但是,正如jordanm已经提到的,这是一种毫无意义的奋进,因为看起来你试图从表中获取所有内容。如果你试图获取所有内容,请将where子句全部删除:

"SELECT * FROM students"

相关问题