Jul-17-2020, 06:48 PM
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: