-1

I try to add information into database (sqlite3, python 3.6) I created the tkinter widget without a problem, but I had a problem to save the information collected from the entry widget and to store it in the database

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import sqlite3

master=Tk()
master.geometry('450x300')
master.title('add into database')

var_Name=StringVar()
var_Street=StringVar()
var_City=StringVar()

CreateDataBase = sqlite3.connect('MyDataBase.db')

QueryCurs = CreateDataBase.cursor()
#to create a table (Clients)
def CreateTable():
    QueryCurs.execute('''CREATE TABLE IF NOT EXISTS Clients
    (id INTEGER PRIMARY KEY, Name TEXT,Street TEXT,Town TEXT)''')

def AddEntry(Nom,Rue,Ville):

    QueryCurs.execute('''INSERT INTO Clients (Name,Street,Town)
    VALUES (?,?,?)''',(str(var_Name.get()),str(var_Street.get()),str(var_City.get())))

CreateTable()

label_Name=Label(master, text ='enter your name  :')
label_Name.pack()  
entry_Name=Entry(master,textvariable=var_Name)
entry_Name.pack()   
label_Street=Label(master, text ='enter street :')
label_Street.pack()
entry_Street=Entry(master,textvariable=var_Street)
entry_Street.pack() 
label_City=Label(master, text ='enter city :')
label_City.pack()
entry_City=Entry(master,textvariable=var_City)
entry_City.pack()   
btn_Valider=Button(master,text='validate', command=AddEntry)
btn_Valider.pack()

CreateDataBase.commit()  
QueryCurs.execute('SELECT * FROM Clients ORDER BY id DESC')

for i in QueryCurs:
    print ("\n")
    for j in i:
        print (j)

QueryCurs.close()

master.mainloop()

when I test the code I have this error:

TypeError: AddEntry() missing 3 required positional arguments: 'Name', 'Street', and 'City'

I need help thanks

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
atlass218
  • 9
  • 6
  • `def AddEntry(Nom,Rue,Ville)` but in Button: `command=AddEntry` – barbsan Jan 04 '19 at 11:36
  • Consider to redesign to this: [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759). Concentrate your `SQL` part into a own `class` or at least a own `function`. – stovfl Jan 04 '19 at 11:55

1 Answers1

0

I modify the function "AddEntry" by this new code :

def AddEntry():
    city_get=str(var_City.get())
    name_get=str(var_Name.get())
    street_get=str(var_Street.get())
    QueryCurs.execute('''INSERT INTO Clients (Name,Street,City) VALUES (?,?,?)''',(name_get,street_get,city_get))
    CreateDataBase.commit()

and I add this new function and button to display the content of database.

btn_display_database=Button(master,text='display content of database', command=display_database)
btn_display_database.pack()

def display_database():
    QueryCurs.execute('SELECT * FROM Clients ORDER BY id DESC') 
    for resultat in QueryCurs.fetchall():
        print(resultat)

the script works if I don't write in the end of the script before : master.mainloop() this two commands to close cursor and connection:

QueryCurs.close()
CreateDataBase.close()

if I write this two last command, I have error below when I run the script :

sqlite3.ProgrammingError: Cannot operate on a closed cursor
atlass218
  • 9
  • 6