Python Forum
Help with PySimpleGUI INSERT INTO sqlite3 database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with PySimpleGUI INSERT INTO sqlite3 database
#1
Brand new to the forum. I only started using Python this week. I'm trying to make a GUI where our operators can scan a barcode into the GUI (and a few other selections) and then pass those user input variables to the sqlite3 database. In the INSERT INTO statement, if I make the data static (i.e. putting each value on single quotes), then it passes the static info into the db. So I know that my connection to the db is good. All of my input fields have keywords, and inside the program, I can print the keyword results to the screen, so I know that works as well. But I can't seem to use the keywords in the INSERT INTO statement, to pass the user input values into the db. Any help would be appreciated. Thanks...

import PySimpleGUI as sg
import automationhat, sqlite3, datetime, time

# Logic to create the timestamp
con = sqlite3.connect ('BarcodeScans.db')
ts=time.gmtime()
readable=(time.strftime("%Y-%m-%d %H:%M:%S",ts))


#sg.theme('DarkAmber')   # Add a touch of color
# All the stuff inside your window.
layout = [[sg.Text('Scan Router Barcode', size=(30, 1), font=("Helvetica", 25), text_color='white')],
[sg.Text('_'  * 100, size=(70, 1))],
   [sg.Text('Scan Barcode')],
   [sg.InputText(key='-Barcode')],
[sg.Text('Station #')],
[sg.InputCombo(['1', '2', '3', '4', '5', '6', '7', '8', ], size=(10, 8), default_value='1', key='Station')],
[sg.Text('Run Attempt')],
[sg.InputCombo(['1', '2'], size=(10, 3), default_value='1', key='Attempt')],
[sg.Text('Scan Date')],
[sg.InputText(readable, size=(20, 1), key='ScanDate')],
[sg.Button('Ok'), sg.Button('Cancel')]]  #, sg.Button('Customized', button_color=('white', 'green'))

# Create the Window
window = sg.Window('Barcode Tracking', auto_size_text=True, default_element_size=(20, 1)).Layout(layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    print('')
    print(values['-Barcode'])
    print(values['Station'])
    print(values['Attempt'])
    print(values['ScanDate'])
    cursorObj=con.cursor()
    sqlite_insert_query="""INSERT INTO tblScans (Barcode, Station, Attempt, ScanDate) VALUES ('MAD12345', 1, 1, '2020-07-16 12:27:30')"""
    count=cursorObj.execute(sqlite_insert_query)

con.commit()
window.close()
Below is the line that I get stuck on. I feel like I've tried almost every syntax I can think of to get the values passed to the db, but evidently I haven't.

sqlite_insert_query="""INSERT INTO tblScans (Barcode, Station, Attempt, ScanDate) VALUES (['-Barcode'], ['Station'], ['Attempt'], [ScanDate'])"""
With the above line, I'm getting unrecognized token:
Reply


Messages In This Thread
Help with PySimpleGUI INSERT INTO sqlite3 database - by jrbond - Jul-17-2020, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 5,017 Jan-10-2023, 09:51 PM
Last Post: gradlon93
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 7,684 Dec-15-2021, 08:48 PM
Last Post: Hilal
  [Tkinter] TKINTER quiz using sqlite3 database hezza_23 45 21,791 Nov-29-2021, 09:42 PM
Last Post: Hilal
  [Tkinter] sqlite3 insert date rwahdan 1 1,673 Jul-07-2021, 08:58 PM
Last Post: rwahdan
  [PySimpleGUI]How to insert values that were gotten from FilesBrowse into ListBox? trigchen 0 2,892 Dec-30-2019, 06:58 AM
Last Post: trigchen

Forum Jump:

User Panel Messages

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