-1

I'm making a GUI with drop-down menus (OptionMenu from tkinter) in python.

I want to insert the data from the drop-down menus into an SQLite database, eg there's a drop-down menu for choosing your car and I want to insert the user's car choice into the database along with the date for the entry.

I've been checking out how to use tkinter with sqlite in python and Use OptionMenu in Python but I can't get my code to work (see error message).

I'm using Python 3.7.4, Windows 10.

Thanks!

from tkinter import * 
import sqlite3 as sq
import datetime

# Main window
window = Tk()

# Connection to SQLite 
con = sq.connect('Cars.db') 

# Car choice variable and placeholder 
carV = StringVar(window)
carV.set('Choose your car') 

# Car choice dictionary for drop-down menu
carD = {'BMW', 'Volvo', 'Mitsubishi'}

# Car chhoice drop-down menu 
carDDM = OptionMenu(window, carV, carD)
carDDM.place(x=150,y=150)

# Function for inserting car choice from drop-down menu into SQLite 
def SaveCarChoice():

        c.execute('CREATE TABLE IF NOT EXISTS (sql_date TEXT, sql_carV INTEGER)') 
        date = datetime.date(int(year.get()),int(month.get()), int(day.get())) 
        c.execute('INSERT INTO (sql_date, sql_carV) VALUES (?, ?)', (date, carDDM.get()) 
        con.commit()

# Button for user to activate the function inserting data into SQLite                    
carB = Button(window, text="Enter",command=SaveCarChoice()) 
carB.pack() 

window.mainloop()

# ERROR MESSAGE
  File "<ipython-input-5-7b7f66e12fbf>", line 42
    con.commit()
      ^
SyntaxError: invalid syntax
1
  • You are missing one closing ) in the line before, which is ` c.execute(.... And this carDDM.get()` should read carV.get() the variable= from the OptionMenu Commented Nov 27, 2019 at 16:10

1 Answer 1

0

You this i have made some changes.

import tkinter as tk
import sqlite3 as sq
import datetime


# Connection to SQLite
con = sq.connect('Cars.db')

# Car choice dictionary for drop-down menu
carD = ['BMW', 'Volvo', 'Mitsubishi']


root = tk.Tk()
canvas = tk.Canvas(root, height=500, width= 500, bg="white")
canvas.pack()

carV = tk.StringVar(root)
carV.set('Choose your car')

carDDM = tk.OptionMenu(canvas, carV, *carD)
carDDM.pack()

# Function for inserting car choice from drop-down menu into SQLite
def SaveCarChoice():
    c = con.cursor()
    c.execute('CREATE TABLE IF NOT EXISTS CAR (sql_date VARCHAR(20), sql_carV VARCHAR(20) NOT NULL)')
    today = str(datetime.date.today())
    today = today[8:] + '-' + today[5:7] + '-' + today[:4]
    c.execute('INSERT INTO CAR (sql_date, sql_carV) VALUES (?, ?)', (today, carV.get()))
    con.commit()

# Button for user to activate the function inserting data into SQLite
carB = tk.Button(canvas, text="Enter", command=SaveCarChoice)
carB.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.