0

i wrote a code using sqlite3 now i am trying to rewrite the code so it can work with the GUI i created but I cannot add entry

code for FRONTEND

from tkinter import *
import datbase2



def get_selected_row(event):
    global selected_tuple
    index=list1.curselection()[0]
    selected_tuple=list1.get(index)
    e1.delete(0,END)
    e1.insert(END,selected_tuple[1])

    e2.delete(0,END)
    e2.insert(END,selected_tuple[2])

    e3.delete(0,END)
    e3.insert(END,selected_tuple[3])

    e4.delete(0,END)
    e4.insert(END,selected_tuple[4])


    

def view_command():
    list1.delete(0,END)
    for row in datbase2.view():
        list1.insert(END,row)

def search_command():
    list1.delete(0,END)
    for row in datbase2.search(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get()):
        list1.insert(END,row)

def add_command(): 
    datbase2.insert(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get())
    list1.delete(0,END)
    list1.insert(END,(title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get()))

def delete_command():
    datbase2.delete(selected_tuple[0])

def update_command():
    datbase2.update(selected_tuple[0],title_text.get(),Author_text.get(),Year_text.get(),ISBN_text.get())
    print()

window=Tk()

window.wm_title("A.K BookStore v1.0")

l1=Label (window,text="Title")
l1.grid(row=0,column=0)

l2=Label (window,text="Author")
l2.grid(row=0,column=2)

l3=Label (window,text="Year")
l3.grid(row=1,column=0)

l4=Label (window,text="ISBN")
l4.grid(row=1,column=2)

title_text=StringVar()
e1=Entry(window,textvariable=title_text)
e1.grid(row=0,column=1)

Author_text=StringVar()
e2=Entry(window,textvariable=Author_text)
e2.grid(row=0,column=3)

Year_text=StringVar()
e3=Entry(window,textvariable=Year_text)
e3.grid(row=1,column=1)

ISBN_text=StringVar()
e4=Entry(window,textvariable=ISBN_text)
e4.grid(row=1,column=3)

list1=Listbox(window,height=10,width=40)
list1.grid(row=2,column=0, rowspan=6,columnspan=2)

sb1=Scrollbar(window)
sb1.grid(row=2,column=2,rowspan=6)


list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>',get_selected_row)

b1=Button(window,text="View all", width=12, command=view_command)
b1.grid(row=2,column=3)

b2=Button(window,text="Search Entry", width=12, command=search_command)
b2.grid(row=3,column=3)

b3=Button(window,text="Add entry", width=12, command=add_command)
b3.grid(row=4,column=3)

b4=Button(window,text="Update Selected", width=12, command=update_command)
b4.grid(row=5,column=3)

b5=Button(window,text="Delete Selected", width=12, command=delete_command)
b5.grid(row=6,column=3)

b6=Button(window,text="Close", width=12,command=window.destroy)
b6.grid(row=7,column=3)



window.mainloop()

'''

this is the code for the postgresql database

'''

import psycopg2

def create_table():
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY, title text, author text, year integer,isbn integer)")
    conn.commit()
    conn.close()

def insert(title,author,year,isbn):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)",(title,author,year,isbn))
    conn.commit()
    conn.close()


def view():
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("SELECT * FROM book")
    rows=cur.fetchall()
    conn.close()
    return rows

def search(title="",author="",year="",isbn=""):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("SELECT * FROM book WHERE title=%s OR author=%s OR year=%s OR isbn=%s",(title,author,year,isbn))
    rows=cur.fetchall()
    conn.close()
    return rows


def delete(id):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("DELETE FROM store WHERE item=%s", (id,))
    conn.commit()
    conn.close()


def update(id,title,author,year,isbn):
    conn=psycopg2.connect("dbname='BookStore' user='postgres' password='damilare1' host='localhost' port='5432'")
    cur=conn.cursor()
    cur.execute("UPDATE book SET title=%s, author=%s,year=%s, isbn=%s WHERE id=%s", (title,author,year,isbn,id))
    conn.commit()
    conn.close() 

create_table()

insert("Algorithm and Complexity Analysis", "Dr Fatima Fika", 2021, 1234357)
'''

but it gives this error

C:\Users\Marshal\Desktop\python class\GuiTkinter>python BookStoreGUI.py
 Traceback (most recent call last):
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\BookStoreGUI.py", line 2, in <module>
    import datbase2
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\datbase2.py", line 52, in <module>
    insert("Algorithm and Complexity Analysis", "Dr Fatima Fika", 2021, 1234357)
  File "C:\Users\Marshal\Desktop\python class\GuiTkinter\datbase2.py", line 13, in insert
    cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)",(title,author,year,isbn))
psycopg2.errors.NotNullViolation: null value in column "id" of relation "book" violates not-null constraint
DETAIL:  Failing row contains (null, Algorithm and Complexity Analysis, Dr Fatima Fika, 2021, 1234357).
3
  • Why are you trying to insert a NULL for the id of your book table? That makes no sense. Commented Sep 7, 2021 at 14:52
  • recheck the ddl of table book, col id is mandatory... if is a serial (identity) change to force the default seq value: cur.execute("INSERT INTO book VALUES (%s,%s,%s,%s)",(title,author,year,isbn)) Commented Sep 7, 2021 at 14:59
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Sep 12, 2021 at 11:57

1 Answer 1

1

Don't explicitly try to insert NULL as the id (since it's specified to be non-nullable).

Also, you'd probably want to make it an id SERIAL PRIMARY KEY.

cur.execute("INSERT INTO book VALUES (NULL,%s,%s,%s,%s)", (title, author, year, isbn))

should be

cur.execute("INSERT INTO book VALUES (%s,%s,%s,%s)", (title, author, year, isbn))

and to be explicit,

cur.execute("INSERT INTO book (title, author, year, isbn) VALUES (%s,%s,%s,%s)", (title, author, year, isbn))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.