Hello
Can’t find a way to get my code to work… probably simply homework project but can’t find it. My next assignment starts with this code working so…
I need to insert a query out of entry fields into a sqlite database file. This is my code:
I speak Dutch so that is why some code (names) is not in English.
With this code I get a syntax error on the “cursor.execute” line, but I also got other errors because I keep looking for the code to work so changing a lot. Making beginner’s mistakes probably…
from tkinter import *
import sqlite3
root = Tk()
root.geometry('500x300')
root.title('Registration Form')
voor_naam = StringVar()
tussen_voegsel = StringVar()
achter_naam = StringVar()
mo_biel = StringVar()
def database():
voornaam = voor_naam.get
tussenvoegsel = tussen_voegsel.get
achternaam = achter_naam.get
mobiel = mo_biel.get
conn = sqlite3.connect("SQLite_Python.db")
with conn:
cursor = conn.cursor()
cursor.execute(insert into persoon(voornaam, tussenvoegsel, achternaam, mobiel) values(voornaam, tussenvoegsel, achternaam, mobiel));
conn.commit()
reg_label = Label(root,text="Registration Form", width =20, font=("bold", 20))
reg_label.place(x=90, y=53)
voor_naam_label = Label(root,text="Voornaam", width =20, font=("bold", 10))
reg_label.place(x=68, y=130)
voor_naam_entry = Entry(root,textvar=voor_naam)
voor_naam_entry.place(x=240, y=130)
tussenv_label = Label(root,text="Tussenvoegsel", width =20, font=("bold", 10))
tussenv_label.place(x=68, y=160)
tussenv_entry = Entry(root,textvar=tussen_voegsel)
tussenv_entry.place(x=240, y=160)
achter_naam_label = Label(root,text="Achternaam", width =20, font=("bold", 10))
achter_naam_label.place(x=68, y=190)
achter_naam_entry = Entry(root,textvar=achter_naam)
achter_naam_entry.place(x=240, y=190)
mo_biel_label = Label(root,text="Mobiel", width =20, font=("bold", 10))
mo_biel_label.place(x=68, y=220)
mo_biel_entry = Entry(root,textvar=mo_biel)
mo_biel_entry.place(x=240, y=220)
button = Button(root, text="Submit", width=20,bg='brown', fg='black', command=database).place(x=180, y=250)
root.mainloop()
I also tried this code instead of the “conn part” of the Def database, but that didn’t work either…
def insert_persoon:
try:
sqliteConnection =sqlite3.connect("SQLite_Python.db")
insert_persoon_query = insert into persoon(voornaam, tussenvoegsel, achternaam, mobiel) values(voornaam, tussenvoegsel, achternaam, mobiel);
cursor = sqliteConnection.cursor()
print("Successfully connected to SQLite")
cursor.execute(insert_persoon_query)
sqliteConnection.commit()
print('Persoon ingevoerd')
cursor.close()
except sqlite3.Error as error:
print("Error while creating a sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
Thank you so much in advance for looking at my code and answering my topic.
Mattaqua