MySQL INSERT statement in Python - python

I am trying to use Python to insert into MySQL database, but I have an auto-increment column (TeamID). I am using the lines below and it works like a charm. BUT I would like to not specify the TeamID in my Python script as it is an auto-increment
try:
cur.execute ("INSERT INTO teams values (%d, '%s', %d, '%s')" % (11,"Sevilla", 7, "Jorge Sampaoli"))
db.commit()
this works perfectly
How can I get rid of the first %d and 11 please? I want this value to be added automatically via the script
any help is very much appreciated
EDIT:
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="username", # your username
passwd="password", # your password
db="dbname") # name of the data base
cur = db.cursor()
try:
cur.execute ("INSERT INTO teams values ('%s', %d, '%s')" % ("Sevilla", 7, "Jorge Sampaoli"))
db.commit()
except Exception as e:
print("Rolling back")
print(e)
db.rollback()
db.close()

Issue is now resolved
I did specify the column names but didn't notice I need to use %s for all columns including int values. As below:
cur.execute("INSERT INTO teams (TeamName, CountryID, TeamManager) values (%s,%s,%s)", ('Everton', 1, 'Ronald Koeman'))

Try
INSERT INTO teams (name, numb, player) VALUES ('%s', %d, '%s')
I.e.explicitly list columns.
Also PLEASE don't do it like this -- instead of doing '%s' you really need to use prepared statements,I think in Python it is something like:
cursor.execute("INSERT INTO teams (name, numb, player) VALUES (%s, %d, %s)", ['Sevilla', 7, 'John Smith'])
Read up on SQL injections.

import sqlite3
def insert_data(lVideoList, gate_id):
connection = sqlite3.connect('db.sqlite',
detect_types=sqlite3.PARSE_DECLTYPES |
sqlite3.PARSE_COLNAMES)
cursor = connection.cursor()
success = 200
# gateid = 1
default_value = 0
for gate_id in range(no_of_gate):
gate_id = i+1
for videofilename in lVideoList:
print("videofilename: ", videofilename)
insert_query = ("INSERT INTO dailyfootfall(csv_name, video_download, processed, footfall, send_status, "
"male_footfall, send_status_male, female_footfall, send_status_female, gate_id,outsiders, send_status_outsiders) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)")
cursor.execute(insert_query,[videofilename, success, success, default_value, success, default_value,
success, default_value, success, gate_id, default_value, success])
print("Data Inserted Successfully !")
connection.commit()
cursor.close()
connection.close()
if __name__ == "__main__":
lVideoList = getCompleteVideoList("2022_01_24", "10:00", "22:00")
no_of_gate = 3
insert_data (lVideoList, gate_id)
print("default_value inserted!!!!")

Related

SQLite + Python Data not saved in DB file

This is my first project with SQLite.
The code runs perfect I checked and the lines look perfect.
I supposed that due to lack of knowledge of SQLite I'm making a mistake.
Problem: The code runs perfect no problem. But when I finish it doesn't print the values or even save the values in the .db file.
Full Code:
import sqlite3
import datetime
import time
conn = sqlite3.connect('covid.db')
c = conn.cursor()
def create_table():
c.execute('''CREATE TABLE IF NOT EXISTS
covidTrack(
name TEXT,
email TEXT,
ph_number INTEGER,
datestamp TEXT,
keyword TEXT)''')
i_name = input('Please insert FULL NAME : \n ...')
i_email = input('Please insert EMAIL : \n ...')
i_number = input('Please insert PHONE NUMBER : \n ...')
print('Your data has been saved for acelerated contact, thank you.')
time.sleep(3)
def data_entry():
c.execute('INSERT INTO covidTrack VALUES(?,?,?)',
(i_name, i_email, i_number))
conn.commit()
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
def read_from_db():
c.execute('''SELECT * FROM covidTrack
WHERE datestamp
BETWEEN "2021-02-06 14:50:00" AND "2021-02-06 15:00:00"''')
conn.commit()
for row in c.fetchall():
print(row)
create_table()
data_entry()
dynamic_data_entry()
read_from_db()
c.close()
conn.close()
I suppose if something wrong with the way I use conn.commit().
import sqlite3
import datetime
import time
conn = sqlite3.connect('covid.db')
c = conn.cursor()
def create_table():
c.execute('''CREATE TABLE IF NOT EXISTS
covidTrack(
name TEXT,
email TEXT,
ph_number INTEGER,
datestamp TEXT,
keyword TEXT)''')
i_name = input('Please insert FULL NAME : \n ...')
i_email = input('Please insert EMAIL : \n ...')
i_number = input('Please insert PHONE NUMBER : \n ...')
print('Your data has been saved for acelerated contact, thank you.')
time.sleep(3)
def data_entry():
date, keyword = dynamic_data_entry()
c.execute('INSERT INTO covidTrack VALUES(?, ?, ?, ?, ?)', (i_name, i_email, i_number, date, keyword))
conn.commit()
def dynamic_data_entry():
keyword = 'nameofvenue'
date = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%M-%D %h:%m:%s'))
return date, keyword
def read_from_db():
c.execute('''SELECT * FROM covidTrack''')
conn.commit()
create_table()
data_entry()
read_from_db()
for row in c.fetchall():
print(row)
c.close()
conn.close()
change the code below (make the commit call part of the function that insert the data). Do it in dynamic_data_entry as well
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
to
def dynamic_data_entry():
keyword = nameofvenue
date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%M-%D %h:%m:%s'))
c.execute('INSERT INTO covidTrack VALUES(date, keyword)')
conn.commit()
You do not actually commiting your executes. Move conn.commit after actual executes.

Insert some multiple records to mysql db using python 3

I want to insert multiple records to a mysql db using python. I use mysql.connector. but I receive error. when I try to insert a record without formatting it works but multiple line with formatting doesn't!
I've used ? instead of %s but I still receive this error.
my_nodes = []
myconnection = mysql.connector.connect(
host='127.0.0.1', user='root', passwd='1234', db='job_graph1')
mycursor=myconnection.cursor()
for nodes in range(1, 33):
weight = random.randint(0, 100)
my_record = (nodes, weight, False, False)
my_nodes.append(my_record)
sqlqu = "INSERT INTO t_node(n_id,n_weight,is_entry,is_exit) VALUES(%S, %S, %s, %s)"
mycursor.executemany(sqlqu, my_nodes)
the error I receive is:
Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; 'tuple' object cannot be interpreted as an integer
So you need to remove %S in your sql request.
Because it causes this error :
print("(%s, %S)"%(1, 2))
ValueError: unsupported format character 'S' (0x53) at index 6
So instead of VALUES(%S, %S, %s, %s) use VALUES(%s, %s, %s, %s)

how to Make a sqlite3 database in python with 1 row

I was wondering if there is way to make a sqlite3 database in python with a Single row (record) of data and when i execute the code again it won't be made twice.
So far I have tried with to code:
conn = sqlite3.connect('db_M.db')
c = conn.cursor()
no1 = "Null"
no2 = "Null"
no3 = "Null"
value = 1
try:
c.execute("CREATE TABLE IF NOT EXISTS M_Memory(Name TEXT, Person TEXT, memory TEXT, value REAL)")
if True:
c.execute("INSERT INTO M_Memory(Name, Person, memory, value) VALUES (?, ?, ?, ?)",
(no1, no2, no3, value))
except:
print(" foo ")
conn.commit()
i solved my own problem .. please if you have any better code share it .. thanks guys
import sqlite3
conn = sqlite3.connect('db_M.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS M_Memory(Name TEXT, Person TEXT, memory TEXT, value REAL)")
text10 = input("what do you want to call the memory ?\n")
text11 = input("what or who the memory will be about?\n")
text12 = input("what do you want me to save in it ?\n")
def Get_The_Max_Function():
Max_Value = []
c.execute('SELECT MAX(value) FROM M_Memory')
lolo = c.fetchone()
if lolo[0] is None:
lolo = 0
Max_Value.insert(0, lolo)
else:
x = int((lolo[0]))
Max_Value.insert(0, x)
return Max_Value
def dynamic_data_entry(Max_Value):
Name = text10
Person = text11
memory = text12
value = Max_Value[0] + 1
c.execute("INSERT INTO M_Memory(Name, Person, memory, value) VALUES (?, ?, ?, ?)",
(Name, Person, memory, value))
conn.commit()
dynamic_data_entry(Get_The_Max_Function())
c.close()
conn.close()
Try this one:
c.execute("INSERT INTO M_Memory(Name, Person, memory, value)
SELECT ?, ?, ?, ?
EXCEPT
SELECT Name, Person, memory, value from M_Memory",
(no1, no2, no3, value) )
EXCEPT, which is MINUS in other databases (such as Oracle), makes sure the row will not not be inserted if another row is already there with all values equal.

Python MySQL Query Format

I'm sure I'm making a simple mistake, but I be darned if I can figure it out. I am making a temperature/humidity monitor using a RPi and a DHT22. Code is in python. Works great. I'd like to dump the variable data collected into a MySQL db, but my insert query keeps failing. I can insert straight strings, but can't seem to figure out how to reference the variables. Here is my code
import time
time.sleep(2)
import MySQLdb
temperature = 60.0
humidity = 30.0
IP_Add = "123.456.78.9"
location = "basement"
name = "home"
while True:
humidity = humidity
temperature = temperature
fTemperature = (temperature * 9 / 5) + 32
name = 'home'
if humidity is not None and temperature is not None:
print('Temp={1:0.1f} Humidity={0:0.1f}%'.format(temperature, humidity))
else:
print('Whoops')
myDB = MySQLdb.connect(host="localhost", port = 3306, user = "root", passwd = "********", db = "PLAYTHING")
cur = myDB.cursor()
try:
#query1 = "INSERT INTO `testdata`(`Name`) VALUES (name)"
#query2 = "INSERT INTO `testdata`(`Name`, `Location`) VALUES (name, location)"
#query3 = "INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)VALUES (%s, %s, %s)", ('No_ID2', 'basement', 30.5)
query4 = "INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)VALUES {0} {1} {2}".format ('No_ID2', 'basement', 30.5)
#query5 = "INSERT INTO testdata(`Name`, `Location`, `T-Reading`, `H-Reading`) VALUES ('Friday3', 'location', 72.5, 29.6)"
cur.execute(query)
myDB.commit()
print ("Commit Sucessful")
except (MySQLdb.Error, MySQLdb.Warning) as e:
print(e)
cur.close()
myDB.close()
time.sleep(10)
I have checked the MySQLdb docs at https://mysqlclient.readthedocs.io/user_guide.html#functions-and-attributes
which offers this as a guide
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)
VALUES (%s, %s, %s, %s, %s)""",
[
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
] )
but that seems not to work for me.
Query 1 and 2 execute but enter no data, col's 3 and 4 are NULL.
Query 3 gives me this message "TypeError: query() argument 1 must be string or read-only buffer, not tuple"
Query 4 enters no data and gives me this: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'No_ID2 basement 30.5' at line 1")
Query 5 is successful, but doesn't solve the problem of getting the variables from the program and inserting them into the db.
If someone would point out my error I would appreciate it.
Issues with the queries:
#1 and #2: name and location in VALUES (name, location) are considered as column names in database, thus no data.
#3: As Ilja pointed out, the tuple should be in execute() call. This should be the way to go.
query3 = ("INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)"
+ " VALUES (%s, %s, %s)")
cur.execute(query3, ('No_ID2', 'basement', 30.5))
#4: To put value directly into VALUES, string must be quoted. Below is the right format. Note: This is for experimental only as it pose SQL Injection security risk.
query4 = ("INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)"
+ " VALUES ('{0}', '{1}', {2})".format (
'No_ID2', 'basement', 30.5
))
#5: All values in SQL statement are constant.

Python insert SQLite

i have the following problem:
i want to insert the temperature of my RPI using SQLite3 and Python.
The python script that i want to use:
import subprocess
import os
import sqlite3 as lite
import datetime
import sys
import time
def get_temperature():
"Returns the temperature in degrees C"
try:
s = subprocess.check_output(["cat","/sys/class/thermal/thermal_zone0/temp"])
return s[:-1]
except:
return 0
try:
con = lite.connect('/www/auslastung.s3db')
cur = con.cursor()
temp = int(get_temperature())
zeit = time.strftime('%Y-%m-%d %H:%M:%S')
cur.execute("INSERT INTO 'temperatur' ('Wert', 'Zeit') VALUES (%s, %s)", (temp, zeit))
con.commit()
except lite.Error, e:
if con:
con.rollback()
print "Error %s" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
Every time i want to run this, i just get the error:
Error near "%": syntax error
What should i do to solve this?
Replace
cur.execute("INSERT INTO 'temperatur' ('Wert', 'Zeit') VALUES (%s, %s)", (temp, zeit))
with
cur.execute("INSERT INTO 'temperatur' ('Wert', 'Zeit') VALUES (?, ?)", (temp, zeit))
There is also a problem with your finally clause. It will fail with the error NameError: name 'con' is not defined if con is never assigned to in the first place (e.g., if the directory /www/ does not exist, so con = lite.connect('/www/auslastung.s3db') fails). You could do the following to avoid this issue:
con = None
try:
# ...
except lite.Error, e:
if con is not None:
con.rollback()
# ...
finally:
if con is not None:
con.close()
You can also replace:
cur.execute("INSERT INTO 'temperatur' ('Wert', 'Zeit') VALUES (%s, %s)", (temp, zeit))
with
cur.execute("INSERT INTO 'temperatur' ('Wert', 'Zeit') VALUES (%s, %s)" % (temp, zeit))
Either #nwk or my answer should work depending on preference.

Categories

Resources