I can't recover Sql data from combobox. Error: 'NoneType' object is not subscriptable

Viewed 144

By selecting an item in the combobox, I would like to retrieve the database row that has the same name selected by the combobox in one of the fields.

For example, if in the combobox there is Tokyo, Madrid, London, Paris (obtained from the "example" column of the database) and I select Tokyo, I would like the row that has the text "Tokyo" in the "example" column to be searched in the database table. Then print the row separately.

I get the error when I try and retrieve and print the data:

TypeError: 'NoneType' object is not subscriptable

While tweaking the code a bit and trying to fix it, I no longer get the error, but the variables are printed as None, None, None, None.

I don't need the function ad i don't need a button. I would simply like that by selecting the combobox, the variables are printed.

Can anyone help me? Thank you

import sqlite3
from tkinter import ttk
import tkinter as tk

root = tk.Tk()
root.geometry('400x400')
root.config(bg="gray")
root.state("normal")
  
conn = sqlite3.connect('....')
cursor = conn.cursor() 

def view_combobox():
    cursor.execute('SELECT example FROM material LIMIT 2 OFFSET 2')
    values = [row[0] for row in cursor]    
    return values

example=ttk.Combobox(root, width = 21)
example.place(x=10, y=10)
example.set("Select")
example['value'] = view_combobox()
            
select_example__button3 = example.get()

#################################################
#PROBLEM IS HERE
if select_example__button3:

    cursor.execute('SELECT a, b, c, d FROM material WHERE example=?', (select_example__button3,))
    value = cursor.fetchone()

    print(value[0], value[1], value[2], value[3], value[4])     

Database

CREATE TABLE "material" (
    "id"    INTEGER,
    "a" INTEGER,
    "b" INTEGER,
    "c" INTEGER,
    "d" INTEGER,
    "example" TEXT,
    PRIMARY KEY("id")
       

NOTE: I have tested that if I insert the condition inside a function and then call the function with a button, I no longer receive the error and the variables are printed correctly. But it was just a test, I DON'T NEED THE FUNCTION, AND I DON'T NEED A BUTTON. I would simply like that by selecting the combobox, the variables are printed

2 Answers

Have you checked the value return from cursor.fetchone(). If due to any reason if it returns empty resultset it returns None. So check the value obtained from cursor.fetchone() if it is not None then only print the value as

cursor.execute('SELECT a, b, c, d FROM material WHERE example=?', (select_example__button3,))
value = cursor.fetchone()
if value is not None:

    print(value[0], value[1], value[2], value[3], value[4]) 

The error TypeError: 'NoneType' object is not subscriptable is telling you that you're trying to use a subscript on a variable that has the value None.

In your case it is the var variable. It is None because you are calling cursor.execute about a millisecond after creating the combobox. The user won't have seen the combobox much less had a chance to pick a value from it. Therefore, the value is going to be what you set it a few statements prior to the empty string and thus, the SQL statement won't find any matches.

I would simply like that by selecting the combobox, the variables are printed

To run a function when a combobox is selected you can bind to the <<ComboboxSelected>> event. It might look something like this:

def select_example(event):
    selected_value = example.get()
    cursor.execute('SELECT a, b, c, d FROM material WHERE example=?', (selected_value,))
    value = cursor.fetchone()
    if value:
        print(value[0], value[1], value[2], value[3], value[4])

example.bind("<<ComboboxSelected>>", select_example)
Related