Inserting variable stored data into SQLite3 - Python 3

2024/11/15 22:58:53

I have been reading information on how to insert data into a database using data stored in a variable. I have not been able to get my data to load to my database and I am not sure why.

The program is written to check for an existing DB and if it does not exist than it creates it along with the tables and columns required, that all works fine.

I have tried to follow the methods from the various tutorials I have been reading but I must be missing something or doing something incorrectly.

The database and table creates properly (I did not include that part of the code, it is executed at the start of the program). Further into my program code I am using the following routine to enter the user input data by clicking of the "Submit" button

Button routine:

submit = Button(window3, font=('arial',12,'bold'), text='Submit', width=12, height=1, bg='aliceblue', fg='steel blue', command = Submit
)

My Submit Routine:

def Submit():connect = sqlite3.connect('SSRB.db')connect.execute('''INSERT INTO ssrb (date, time_in, time_out, company, plate, province, driver, pass1,pass2, pass3, deliver, contact, entry_by, pi_yes, pi_no, pi_violations, pi_done_by,vi_yes, vi_no, vi_violations, vi_done_by)VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s), (Date, TimeIn, TimeOut, Company, Plate, Province, Driver, Pass1, Pass2, Pass3,Delivery, Contact, EntryBy, PIYES, PINO, PIFound, PIDoneBy, VIYES, VINO,VIFound, VIDoneBy)''')connect.commit()connect.close()

The columns in the table are listed after the INSERT, the %s is placed after the VALUES and the variables holding the user input is listed last.

I am not seeing where I am going wrong... can someone please point out what I am doing incorrectly?

Much thanks as always.

Answer

Instead of

connect.execute('''
INSERT INTO ssrb (date, time_in, time_out, company, plate, province, driver, pass1,pass2, pass3, deliver, contact, entry_by, pi_yes, pi_no, pi_violations, pi_done_by,vi_yes, vi_no, vi_violations, vi_done_by)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s), (Date, TimeIn, TimeOut, Company, Plate, Province, Driver, Pass1, Pass2, Pass3,Delivery, Contact, EntryBy, PIYES, PINO, PIFound, PIDoneBy, VIYES, VINO,
VIFound, VIDoneBy)
''')

use

connect.execute('''
INSERT INTO ssrb (date, time_in, time_out, company, plate, province, driver, pass1,pass2, pass3, deliver, contact, entry_by, pi_yes, pi_no, pi_violations, pi_done_by,vi_yes, vi_no, vi_violations, vi_done_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)''', (Date, TimeIn, TimeOut, Company, Plate, Province, Driver, Pass1, Pass2, Pass3,Delivery, Contact, EntryBy, PIYES, PINO, PIFound, PIDoneBy, VIYES, VINO,
VIFound, VIDoneBy)
)

They are 2 differences:

  1. Symbol ? instead of %s as it is not a Python's syntax for the string's .format() method (or older % notation) but the DB-API’s parameter substitution symbol.
  2. Ending ''' are moved up - where the INSERT INTO (parametrized) statement really ends.
    (The substitution itself is performed by the .execute() method by its 2nd parameter - a tuple provided by you.)
https://en.xdnf.cn/q/119266.html

Related Q&A

Print specific line in a .txt file in Python?

I have got a .txt file that contains a lot of lines. I would like my program to ask me what line I would like to print and then print it into the python shell. The .txt file is called packages.txt.

EOF error with both exec_command and invoke_shell method in paramiko

I am trying to execute a command on linux server from my windows machine using python paramiko , I used both of the methods1.exec_command2.invoke_shellBoth of them giving EOF error.import paramiko impo…

Trying to simulate an Overwatch loot box opening program in Python

So basically I am trying to recreate opening a loot box in Overwatch into a runnable program in python. Im trying to make it take four random items in an array and display them each time the user types…

How to remove extra commas from data in Python

I have a CSV file through which I am trying to load data into my SQL table containing 2 columns. I have 2 columns and the data is separated by commas, which identify the next field. The second column c…

How to linearize the sum of a product of two decision variables in LP?

Being new to Linear Programming and Gurobi, I am dealing with an Integer Linear program where I have two binary decision variables defined as B[u_v, x_y] and A[u_x], I am trying to implement this const…

Basic calculator program in python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why am I receiving ERROR 404 - when attempting to use Python Flask

I have been following this tutorial: https://kb.objectrocket.com/postgresql/scrape-a-website-to-postgres-with-python-938 My app.py file looks like this (taken from the above tutorial): from flask impor…

Merge the dataframes dynamically

I am extracting data from APIs to generate a report. But the number of APIs are dynamic for each report. It can be 1,2,3,5 etc. Once we get the data , we need to store the data as dataframe, to generat…

Read an xml element using python

I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.for example:<Actions><ActionGroup enabled="…

Using .rsplit() not giving desired results

I want to manipulate a string using .rsplit() so that anything after the last comma in a string of data is split using commas. As an example:,000...should be changed to:,0,0,0,In order to this I am usi…