Python Forum
finding problems connecting python to sqlite
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding problems connecting python to sqlite
#1
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
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help with data analysing with python and sqlite Hardcool 2 365 Jan-30-2024, 06:49 AM
Last Post: Athi
  Connecting to LDAP through Python ranjansanyal2007 0 418 Oct-09-2023, 05:21 PM
Last Post: ranjansanyal2007
  python sqlite autoincrement in primary column janeik 6 1,156 Aug-13-2023, 11:22 AM
Last Post: janeik
  Help with subtracting values using SQLite & Python Extra 10 3,411 May-10-2022, 08:36 AM
Last Post: ibreeden
  [Solved]Help with search statement-SQLite & Python Extra 1 1,052 May-06-2022, 07:38 PM
Last Post: Extra
  Help With Python SQLite Error Extra 10 15,046 May-04-2022, 11:42 PM
Last Post: Extra
  Python Sqlite georgebijum 0 1,055 May-04-2022, 10:12 AM
Last Post: georgebijum
  Connecting c++ and python Mawixy 1 1,170 Apr-17-2022, 02:34 PM
Last Post: deanhystad
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,691 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,137 Jun-29-2020, 08:51 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020