Hi! In general im new with python. I'm making my first tkinter app, but for some reason im not able to insert entry into the database.
I wanted to insert manually,
sqlite3.OperationalError: no such table: contacts. The database.db file it is in the same directory with the other py files. What could be the problem?
I will appreciate any help, thank you!
![[Image: sc1.png]](https://i.ibb.co/bgn3XSn/sc1.png)
UPDATE:
It seems my VisualStudioCode or Python doesn't want to execute query to the database.
I run the test bellow, it will create database.db and tables, but WONT insert any query. I run the command directly in 'DB Browser for SQLITE' it runs ok and insert data, what should I do?
I wanted to insert manually,
con.execute("INSERT INTO contacts (contact_name, contact_surname) VALUES ('Maryan', 'Maryan', '[email protected]', '2151245123','Maryan');")but I'm getting the following error:sqlite3.OperationalError: no such table: contacts. The database.db file it is in the same directory with the other py files. What could be the problem?
I will appreciate any help, thank you!from sqlite3.dbapi2 import connect
from tkinter import messagebox
from tkinter import *
import sqlite3
con = sqlite3.connect('database.db')
cur = con.cursor()
class AddPeople(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.geometry('650x750+550+200')
self.title('Add Contact')
self.resizable(False, False)
# Frames
self.top = Frame(self, height = 150, bg = 'white')
self.top.pack(fill = X)
self.bottomFrame = Frame(self, height = 500, bg = '#adff2f')
self.bottomFrame.pack(fill = X)
# Heading, image and date - TOP frame
self.top_image = PhotoImage(file = 'addressbook/icons/addperson.png')
self.top_image_lbl = Label(self.top, image = self.top_image, bg = 'white')
self.top_image_lbl.place(x = 120, y = 10)
self.heading = Label(self.top, text = 'New Contact', font = 'Helvetica 15 bold', fg = '#FFA500', bg = 'white')
self.heading.place(x = 260, y = 60)
### Labels and Entries ###
# Name
self.lbl_name = Label(self.bottomFrame, text = 'Name', font = 'Helvetica 15 bold', fg = 'white', bg = '#adff2f')
self.lbl_name.place(x = 40, y = 40)
self.ent_name = Entry(self.bottomFrame, width = 30, bd = 2)
self.ent_name.insert(0, '')
self.ent_name.place(x = 150, y = 45)
# Surname
self.lbl_surname = Label(self.bottomFrame, text = 'Surname', font = 'Helvetica 15 bold', fg = 'white', bg = '#adff2f')
self.lbl_surname.place(x = 40, y = 80)
self.ent_surname = Entry(self.bottomFrame, width = 30, bd = 2)
self.ent_surname.insert(0, '')
self.ent_surname.place(x = 150, y = 85)
# Email
self.lbl_email = Label(self.bottomFrame, text = 'Email', font = 'Helvetica 15 bold', fg = 'white', bg = '#adff2f')
self.lbl_email.place(x = 40, y = 120)
self.ent_email = Entry(self.bottomFrame, width = 30, bd = 2)
self.ent_email.insert(0, '')
self.ent_email.place(x = 150, y = 125)
# Phone
self.lbl_phone = Label(self.bottomFrame, text = 'Phone', font = 'Helvetica 15 bold', fg = 'white', bg = '#adff2f')
self.lbl_phone.place(x = 40, y = 160)
self.ent_phone = Entry(self.bottomFrame, width = 30, bd = 2)
self.ent_phone.insert(0, '')
self.ent_phone.place(x = 150, y = 165)
# Address
self.lbl_address = Label(self.bottomFrame, text = 'Address', font = 'Helvetica 15 bold', fg = 'white', bg = '#adff2f')
self.lbl_address.place(x = 40, y = 300)
self.address = Text(self.bottomFrame, width = 23, height = 15, wrap = WORD)
self.address.place(x = 150, y = 200)
# Button
button = Button(self.bottomFrame, text = 'Add Contact', command = self.addPerson)
button.place(x = 270, y = 460)
self.lift()
def addPerson(self):
name = self.ent_name.get()
surname = self.ent_surname.get()
email = self.ent_email.get()
phone = self.ent_phone.get()
address = self.address.get(1.0, 'end-1c')
# Database query
if (name and surname and email and phone and address != ""):
try:
query = "INSERT INTO contacts (contact_name, contact_surname, contact_email, contact_phone, contact_address) VALUES(?, ?, ?, ?, ?)"
cur.execute(query, (name, surname, email, phone, address))
con.commit()
con.close()
messagebox.showinfo('Success', 'Sucessfully added into the database', icon = 'info')
except:
messagebox.showerror('Error', 'Can Not be added to the database!', icon = 'warning')
else:
messagebox.showerror('Error', 'The fields are empty!', icon = 'warning')screenshot![[Image: sc1.png]](https://i.ibb.co/bgn3XSn/sc1.png)
UPDATE:
It seems my VisualStudioCode or Python doesn't want to execute query to the database.
I run the test bellow, it will create database.db and tables, but WONT insert any query. I run the command directly in 'DB Browser for SQLITE' it runs ok and insert data, what should I do?
import sqlite3
db = sqlite3.connect('database.db')
cur = db.cursor()
#cur.execute("CREATE TABLE contacts (name TEXT, surname TEXT, email TEXT, phone TEXT, address TEXT)")
cur.execute("INSERT INTO contacts(name, surname, email, phone, address) VALUES ('Example', 'example', '[email protected]','123456789', 'example st.')")
db.commit
