简体   繁体   中英

add into the database by the entry widget

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM