Home > Enterprise >  i got this error sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type. how
i got this error sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type. how

Time:10-15

this my cod

import sqlite3
from tkinter import *
from tkinter import ttk


class root(Tk):
    def __init__(self):
        super().__init__()
        self.bookInfodb = sqlite3.connect("data.db")
        self.c = self.bookInfodb.cursor()
        self.bookInfodb.commit()
        self.c.execute(
            "CREATE TABLE IF NOT EXISTS books (title TEXT, author TEXT, year TEXT, isbn TEXT)"
        )
        self.title("book info")
        self.minsize(500, 320)
        self.configure(background="gray")
        self.datas()

    def addData(self, title, years, author, isbn):

        self.c.execute(
            "INSERT INTO books VALUES (?,?,?,?)",
            ([title, years, author, isbn]),
        )
        self.bookInfodb.commit()

    def datas(self):
        self.title = StringVar()

        self.titleEntry = ttk.Label(self, text="titel")
        self.titleEntry.place(x=41, y=10)

        entry = Entry(self, textvariable=self.title)
        entry.place(x=66, y=10)

        # make years input

        self.years = IntVar()

        self.yearsEntry = ttk.Label(self, text="years")
        self.yearsEntry.place(x=269, y=10)

        entry2 = Entry(self, textvariable=self.years)
        entry2.place(x=300.5, y=10.2)

        # make author input

        self.author = StringVar()

        self.authorEntry = ttk.Label(self, text="author")
        self.authorEntry.place(x=40, y=51)

        entry3 = Entry(self, textvariable=self.author)
        entry3.place(x=80, y=51)

        # make isbn input

        self.isbn = IntVar()

        self.isbnEntry = ttk.Label(self, text="isbn")
        self.isbnEntry.place(x=270, y=52)

        entry_iv = Entry(self, textvariable=self.isbn)
        entry_iv.place(x=297, y=52)

        # buttons

        save_button = Button(
            self,
            text=" save ",
            command=(
                lambda t=self.title, y=self.years, a=self.author, i=self.isbn: self.addData(
                    t, y, a, i
                )
            ),
        )

        save_button.place(x=66, y=150)

        delete_button = Button(self, text="delete")
        delete_button.place(x=215, y=150)

        search_button = Button(self, text="search")
        search_button.place(x=360, y=150)

windows = root()
windows.mainloop()

CodePudding user response:

As typedecker's comment says, you'd need to read the actual values out of the Tk variables. Since you're storing the vars on the instance anyway, you don't need to pass them to the addData function separately. The datas function is also extraneous, and I took the liberty of renaming things so labels are labels and entries are entries, the main class is more sanely named, and getting rid of the wildcard import.

import sqlite3
from tkinter import ttk, StringVar, IntVar, Tk, Entry, Button


class BookApp(Tk):
    def __init__(self):
        super().__init__()
        self.bookInfodb = sqlite3.connect("data.db")
        self.bookInfodb.execute(
            "CREATE TABLE IF NOT EXISTS books (title TEXT, author TEXT, year TEXT, isbn TEXT)"
        )
        self.title("book info")
        self.minsize(500, 320)
        self.configure(background="gray")
        self.title_var = StringVar()
        self.years_var = IntVar()
        self.author_var = StringVar()
        self.isbn_var = StringVar()

        # make title input
        title_label = ttk.Label(self, text="titel")
        title_label.place(x=41, y=10)
        title_entry = Entry(self, textvariable=self.title_var)
        title_entry.place(x=66, y=10)

        # make years input
        years_label = ttk.Label(self, text="years")
        years_label.place(x=269, y=10)
        years_entry = Entry(self, textvariable=self.years_var)
        years_entry.place(x=300.5, y=10)

        # make author input
        author_label = ttk.Label(self, text="author")
        author_label.place(x=40, y=51)
        author_entry = Entry(self, textvariable=self.author_var)
        author_entry.place(x=80, y=51)

        # make isbn input
        isbn_label = ttk.Label(self, text="isbn")
        isbn_label.place(x=270, y=52)
        isbn_entry = Entry(self, textvariable=self.isbn_var)
        isbn_entry.place(x=297, y=52)

        # buttons
        save_button = Button(
            self,
            text=" save ",
            command=self.addData,
        )

        save_button.place(x=66, y=150)

        delete_button = Button(self, text="delete")
        delete_button.place(x=215, y=150)

        search_button = Button(self, text="search")
        search_button.place(x=360, y=150)

    def addData(self):
        self.bookInfodb.execute(
            "INSERT INTO books VALUES (?,?,?,?)",
            (
                self.title.get(),
                self.author_var.get(),
                self.years_var.get(),
                self.isbn_var.get(),
            ),
        )
        self.bookInfodb.commit()


app = BookApp()
app.mainloop()

CodePudding user response:

The error occurs, because you are trying to store an object of tkinter.StringVar, in your sql database.

Since, self.title has been declared as an object of the tkinter.StringVar class here -:

self.title = StringVar()

but, when you store it in your database, perhaps what you want to store, is the string value of the self.title, stringvar, which can be fetched using the .get() method of tkinter.StringVar like so -:

title_value = self.title.get()

Also, since author, years, and all other fields of data being stored into the database, but have been defined as a tkinter.StringVar, the same procedure will have to be followed for all of them.

So, by changing the arguments passed to the function call to the addData() function call, you can fix the problem, like so -:

save_button = Button(
            self,
            text=" save ",
            command=(
                lambda t=self.title.get(), y=self.years.get(), a=self.author.get(), i=self.isbn: self.addData(
                    t, y, a, i
                )
            ),
        )

NOTE:

You can also, alternatively, modify your addData() method to accept tkinter.StringVar type as parameters but then fetch the value when executing the query like so -:

def addData(self, title, years, author, isbn):
    self.c.execute(
         "INSERT INTO books VALUES (?,?,?,?)",
         ([title.get(), years.get(), author.get(), isbn.get()]),
      )
    self.bookInfodb.commit()

Further, as suggested by @AKX in his answer, some modifications to your code can make it look neater, shorter, and perhaps prevent other errors, that are possible due to wildcard imports.

  • Related