0

I am making a simple contact book with tkinter and sqlite3. It has not been completed yet. I am getting an error which I did not get on my previous project. Here is my code below:

Backend:

import sqlite3

def connect():
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS contact (name text, number integer, id integer,email text)")
    conn.commit()

def view():
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("SELECT * FROM contact")
    rows=cur.fetchall()
    conn.close()
    return rows

def search(name="", number="", email="", id=""):
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("SELECT * FROM contact WHERE name=? OR number=? or email=? OR id=?",(name,number,email,id))
    rows=cur.fetchall()
    conn.close()
    return rows

def insert(name="", number="", email="", id=""):
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("INSERT INTO contact VALUES(?,?,?,?)", (name,number,email,id))
    conn.commit()
    conn.close()

def update(name="", number="", email="", id=""):
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("UPDATE contact SET name=?, number=?, email=? WHERE id=?",(name,number,email,id))
    conn.commit()
    conn.close()
def delete(id):
    conn=sqlite3.connect("book.db")
    cur=conn.cursor()
    cur.execute("DELETE FROM contact WHERE id=?",(id,))
    conn.commit()
    conn.close()


connect()

And my frontend:

from tkinter import *
import webbrowser
import backend

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

def add_command():
    backend.insert(name_text.get(), contact_text.get(), email_text.get(),ID_text.get())
    list1.delete(0,END)
    for rows in backend.search(name_text.get(), contact_text.get(), email_text.get(),ID_text.get()):
        list1.insert(rows, END)
    e1.delete(0,END)
    e2.delete(0,END)
    e3.delete(0,END)
    e4.delete(0,END)




window=Tk()

window.wm_title("Contact book")

window.iconbitmap(r'C:\Anirudh\Python\School\project\icon.ico')


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

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

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

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

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

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

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

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

list1=Listbox(window, height=10,width=35)
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)

sb2=Scrollbar(window, orient='horizontal', bg="red")
sb2.grid(row=8,column=1, rowspan=6)

list1.configure(xscrollcommand=sb2.set)
sb2.configure(command=list1.xview)


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)
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)
b4.grid(row=5,column=3)

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

b6=Button(window,text="Whatsapp", width=12, bg="green")
b6.grid(row=7,column=3)

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


window.mainloop()

I have not yet completed it... When I try to add some contact to the database I get the following error.

PS C:\Anirudh\Python\School\project> python frontend.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\aayan\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "frontend.py", line 11, in add_command
    backend.insert(name_text.get(), contact_text.get(), email_text.get(),ID_text.get())
  File "C:\Anirudh\Python\School\project\backend.py", line 28, in insert
    cur.execute("INSERT INTO contact VALUES(?,?,?,?)", (name,number,email,id))
sqlite3.IntegrityError: datatype mismatch

Thanks.

1 Answer 1

1

In the statement

  cur.execute("INSERT INTO contact VALUES(?,?,?,?)", (name,number,email,id))

number and id are strings, but should be integers. You can fix that right there in the insert method with

cur.execute("INSERT INTO contact VALUES(?,?,?,?)", (name,int(number),email,int(id)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks... That worked. Also I found out that removing the ="" in the insert function parameters would solve the problem
Those are default values for the parameters of the function. They only get used if you don't pass the corresponding parameter in the function call. For example if you don't pass id a value, then it becomes an empty string inside the function.

Your Answer

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