Python Forum

Full Version: finding problems connecting python to sqlite
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
please i need you to help me on this. i am trying to connect python 3.7 to sqlite.
below is the code
import sqlite3 
from tkinter import *
import tkinter.messagebox
root = Tk()
root.title('Personal Information')
root.resizable(False,False)
root.geometry('500x500+400+150')

####### connect to the Database############################################
conn = sqlite3.connect("database.db")

###### cursor to move around the Database###############################
c = conn.cursor()

def add_data():
   
    val2 = Nameentry.get()
    val3 = locentry.get()
    val4 = phoneentry.get()

    if val2 == '' or val3 == '' or val4 == '':
        tkinter.messagebox .showinfo ('Warning','Please fill out all boxes')
    
      
   
    sql = "INSERT INTO 'myown'( Name,Location,Phone_Number)VALUES (?,?,?)"
    c.execute(sql,(val2,val3,val4))
    conn.commit()
    tkinter.messagebox .showinfo ('Message','Data Added')





Namelabel = Label(text = 'Name',font = ('ariel,10,bold'),fg = 'black')
Namelabel.place(x = 20,y = 110)
Nameentry = Entry(width = 8,bd = 8,bg ='steelblue',font = ('ariel,10,bold'))
Nameentry.place(x = 100,y = 100)


loclabel = Label(text = 'Location',font = ('ariel,10,bold'),fg = 'black')
loclabel.place(x = 20,y = 150)
locentry = Entry(width = 8,bd = 8,bg ='steelblue',font = ('ariel,10,bold'))
locentry.place(x = 100,y = 150)

phonelabel = Label(text = 'Phone',font = ('ariel,10,bold'),fg = 'black')
phonelabel.place(x = 20,y = 200)
phoneentry = Entry(width = 8,bd = 8,bg ='steelblue',font = ('ariel,10,bold'))
phoneentry.place(x = 100,y = 200)

savebtn = Button(bd = 8,font = ('ariel,10,bold'),fg = 'black',bg = 'steelblue',text = 'SAVE',padx =10, pady = 10,command = add_data)
savebtn.place(x = 200,y = 300)

root.mainloop()
it is showing no error when i run but i am not getting the data into the database. please help me out. thanks
You need string variables for each or your Entry widgets
I have added these as str1, str2, and str3
These also have to be declared in the Entry widget definitions as textvariables
and finally, you didn't create the table myown which I added (you should choose a primary key for this)
import sqlite3 
from tkinter import *
import tkinter.messagebox
root = Tk()
root.title('Personal Information')
root.resizable(False,False)
root.geometry('500x500+400+150')
 
####### connect to the Database############################################
conn = sqlite3.connect("database.db")

###### cursor to move around the Database###############################
c = conn.cursor()

# Need to create table if it doesn't exist
sql = "CREATE TABLE IF NOT EXISTS myown (Name TEXT, Location  TEXT, Phone_Number TEXT);"
c.execute(sql)
 
str2 = tkinter.StringVar()
str3 = tkinter.StringVar()
str4 = tkinter.StringVar()

def add_data():
    # Old:
    # val2 = Nameentry.get()
    # val3 = locentry.get()
    # val4 = phoneentry.get()
    val2 = str2.get()
    val3 = str3.get()
    val4 = str4.get()

    if val2 == '' or val3 == '' or val4 == '':
        tkinter.messagebox .showinfo ('Warning','Please fill out all boxes')

    sql = "INSERT INTO 'myown'( Name,Location,Phone_Number)VALUES (?,?,?)"
    c.execute(sql,(val2,val3,val4))
    conn.commit()
    tkinter.messagebox .showinfo ('Message','Data Added')
 
 
 
 
 
Namelabel = Label(text = 'Name',font = ('ariel,10,bold'),fg = 'black')
Namelabel.place(x = 20,y = 110)
Nameentry = Entry(width = 8,bd = 8, textvariable=str2, bg ='steelblue',font = ('ariel,10,bold'))
Nameentry.place(x = 100,y = 100)
 
 
loclabel = Label(text = 'Location',font = ('ariel,10,bold'),fg = 'black')
loclabel.place(x = 20,y = 150)
locentry = Entry(width = 8,bd = 8, textvariable=str3, bg ='steelblue',font = ('ariel,10,bold'))
locentry.place(x = 100,y = 150)
 
phonelabel = Label(text = 'Phone',font = ('ariel,10,bold'),fg = 'black')
phonelabel.place(x = 20,y = 200)
phoneentry = Entry(width = 8,bd = 8, textvariable=str4, bg ='steelblue',font = ('ariel,10,bold'))
phoneentry.place(x = 100,y = 200)
 
savebtn = Button(bd = 8,font = ('ariel,10,bold'),fg = 'black',bg = 'steelblue',text = 'SAVE',padx =10, pady = 10,command = add_data)
savebtn.place(x = 200,y = 300)
 
root.mainloop()