I need to update a row if a record already exists or create a new one if it dosen't. I undersant ON DUPLICATE KEY will accomplish this using MYSQLdb, however I'm having trouble getting it working. My code is below
cursor = database.cursor()
cursor.execute("INSERT INTO userfan (user_id, number, round VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE user_id =%s, number=%s, round=%s", (user_id, number, round))
database.commit()
primary key is user_id
A parenthesis was missiing. You can also use the VALUES(column) in the ON DUPLICATE KEY UPDATE section of the statement:
cursor = database.cursor()
cursor.execute("""
INSERT INTO userfan
(user_id, number, round)
VALUES
(%s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no need to update the PK
number = VALUES(number),
round = VALUES(round) ;
""", (user_id, number, round) # python variables
)
database.commit()
def insertAndUpdateData(lVideoList, no_of_gate):
connection = sqlite3.connect('db.sqlite',
detect_types=sqlite3.PARSE_DECLTYPES |
sqlite3.PARSE_COLNAMES)
cursor = connection.cursor()
success = 200
unsuccess = 500
default_value = 0
lDefaultEntry = None
for i in range(no_of_gate):
gate_id = i+1
for videofilename in lVideoList:
cursor.execute("SELECT * FROM dailyfootfall WHERE csv_name=? AND gate_id=?", [videofilename, gate_id])
lDefaultEntry = cursor.fetchone()
try:
if lDefaultEntry is not None:
print ('Entry found...!!!')
cursor.execute("UPDATE dailyfootfall SET video_download=?, processed=?, send_status=? ,male_footfall=?, send_status_male=?, "
"female_footfall =?,send_status_female=?, outsiders=?, send_status_outsiders=? "
"WHERE csv_name=? AND gate_id=? AND footfall=0", [unsuccess,unsuccess,unsuccess,default_value,unsuccess,
default_value,unsuccess,default_value,unsuccess,videofilename,gate_id])
print("Data_Updated..!!!")
else:
cursor = connection.cursor()
print ('Entry Not found...!!!')
print("videofilename: ", videofilename)
insert_query = ("INSERT or IGNORE 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, unsuccess, unsuccess, default_value, unsuccess, default_value,
unsuccess, default_value, unsuccess, gate_id, default_value, unsuccess])
print("Data_Inserted..!!")
print("="*20)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print("Entry found: ",exc_type, fname, exc_tb.tb_lineno)
print("Data Inserted Successfully !")
connection.commit()
cursor.close()
connection.close()
if __name__ == "__main__":
lVideoList = ['2022_01_27_10_00_00-2022_01_25_10_30_00', '2022_01_27_10_30_00-2022_01_25_11_00_00',
'2022_01_27_11_00_00-2022_01_25_11_30_00', '2022_01_27_11_30_00-2022_01_25_12_00_00']
no_of_gate = 3
UpdateData(lVideoList, no_of_gate)
print("default_value inserted!!!!")
Related
I end up always with "Incorrect number of bindings supplied" error of SQlite3. I tried (dataset), (dataset,),[dataset].
import requests
import json
import datetime
import sqlite3
#Get tbe Data
url = 'https://opensky-network.org//api/flights/departure?airport=EDDF&begin=1517227200&end=1517230800'
content = requests.get(url).content
dataset = json.loads(content)
#print (dataset)
#Create Table in Sqlite3
try:
db = sqlite3.connect('Flights')
cursor = db.cursor()
cursor.execute('''create table flights(icao24 VARCHAR(50), firstSeen VARCHAR(50), estDepartureAirport VARCHAR(50), lastSeen VARCHAR(50), estArrivalAirport VARCHAR(50), callsign VARCHAR(50), estDepartureAirportHorizDistance VARCHAR(50), estDepartureAirportVertDistance VARCHAR(50), estArrivalAirportHorizDistance VARCHAR(50), estArrivalAirportVertDistance VARCHAR(50), departureAirportCandidatesCount VARCHAR(50), arrivalAirportCandidatesCount VARCHAR(50))''')
except Exception as E:
print('Error:', E)
else:
print ('Table created')
#Insert Date to the Table
try:
cursor.executemany('insert into flights(icao24, firstSeen, estDepartureAirport, lastSeen, estArrivalAirport, callsign, estDepartureAirportHorizDistance, estDepartureAirportVertDistance, estArrivalAirportHorizDistance, estArrivalAirportVertDistance, departureAirportCandidatesCount, arrivalAirportCandidatesCount) values (?,?,?,?,?,?,?,?,?,?,?,?)', (dataset,))
except Exception as E:
print('Error:', E)
else:
db.commit()
print ('Data inserted')
dataset is a list of dictionaries. My assumption is that you want to insert the values from the dictionaries into the table. So, replacing (dataset,) with
(tuple(record.values()) for record in dataset) should produce the desired result (worked here):
...
#Insert Date to the Table
try:
cursor.executemany(
'''
insert into flights (
icao24,
firstSeen,
estDepartureAirport,
lastSeen,
estArrivalAirport,
callsign,
estDepartureAirportHorizDistance,
estDepartureAirportVertDistance,
estArrivalAirportHorizDistance,
estArrivalAirportVertDistance,
departureAirportCandidatesCount,
arrivalAirportCandidatesCount
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''',
(tuple(record.values()) for record in dataset)
)
...
(tuple(record.values()) for record in dataset) is an iterator (actually a generator, which is also an iterater) which is a suitable argument for .executemany():
... The sqlite3 module also allows using an iterator yielding parameters instead of a sequence.
It seems to me that the dictionaries are sorted in the order you need for the insert. If that is not (always) the case you should modify the generator accordingly (fetching the values explicitly by keys in the desired order).
The problem was that you were trying to insert a list of dictionaries into your database instead of inserting each dictionary separately and then using an update statement after that to put them together again. The following code should work for you now. It uses the same structure as your original code but inserts each dictionary one at a time instead of trying to do it all in one go.
# Import required modules
import requests
import json
from bs4 import BeautifulSoup
# Get the data
url = 'https://opensky-network.org//api/flights/departure?airport=EDDF&begin=1517227200&end=1517230800'
content = requests.get(url).content
dataset = json.loads(content)
# Create connection
conn = sqlite3.connect("Flights")
# Insert the data
try:
cursor = conn.cursor()
cursor.execute('''create table flights(icao24 VARCHAR(50), firstSeen VARCHAR(50), estDepartureAirport VARCHAR(50), lastSeen VARCHAR(50), estArrivalAirport VARCHAR(50), callsign VARCHAR(50), estDepartureAirportHorizDistance VARCHAR(50), estDepartureAirportVertDistance VARCHAR(50), estArrivalAirportHorizDistance VARCHAR(50), estArrivalAirportVertDistance VARCHAR(50), departureAirportCandidatesCount VARCHAR(50), arrivalAirportCandidatesCount VARCHAR(50))''')
cursor.executemany('''insert into flights(icao24, firstSeen, estDepartureAirport, lastSeen, estArrivalAirport, callsign, estDepartureAirportHorizDistance, estDepartureAirportVertDistance, estArrivalAirportHorizDistance, estArrivalAirportVertDistance,
departureAirportCandidatesCount, arrivalAirportCandidatesCount) values (?,?,?,?,?,?,?,?,?,?,?,?)''', (dataset,))
except Exception as E:
print('Error:', E)
else:
conn.commit()
print('Data inserted')
# Update the table to include the new information
try:
cursor = conn.cursor()
cursor.execute('''update flights set estDepartureAirport = ?, estDepartureAirportHorizDistance = ?, estDepartureAirportVertDistance = ?, estArrivalAirport = ?, estArrivalAirportHorizDistance = ?, estArrivalAirportVertDistance = ?, departureAirportCandidatesCount = ?, arrivalAirportCandidatesCount = ? where ICAO24 = ?''', (dataset.pop(0).get('estDepartureAirport'), dataset.pop(0).get('estDepartureAirportHorizDistance'), dataset.pop(0).get('estDepartureAirportVertDistance'), dataset.pop(0).get('estArrivalAirport'), dataset.pop(0).get('estArrivalAirportHorizDistance'), dataset.pop(0).get('estArrivalAirportVertDistance'), dataset.pop(0).get('departureAirportCandidatesCount'), dataset.pop(0).get('arrivalAirportCandidatesCount'), dataset.pop(0).get('ICAO24')))
except Exception as E:
print('Error:', E)
else:
conn.commit()
print('Data updated')
I want to add data to my database through python, but I do not know what to do with the ID colum.
I have four colums and I only want to add the last three, the ID is counting up itself.
def add_data(temp, hum):
try:
dt = datetime.datetime.now().replace(microsecond=0).isoformat(' ')
statement = "INSERT INTO messstation (?id?,uhrzeit, luftfeuchtigkeit, raumtemperatur) VALUES (?, ?, ?, ?)"
data = (?id?, dt, hum, temp)
cursor.execute(statement, data)
connection.commit()
except database.error as e:
print(f"Error:{e}")
This should work if the id field is an auto-increment one:
statement = "INSERT INTO messstation (uhrzeit, luftfeuchtigkeit, raumtemperatur) VALUES (?, ?, ?)"
data = (dt, hum, temp)
When I executing the following function, I get the following response:
Failed to insert data into sqlite table: no such table: users
user_id = uuid.uuid4()
save_record.name = name
save_record.score = score
last_time = datetime.now()
try:
conn = sqlite3.connect('OnlineJeopardy.db')
cursor = conn.cursor()
print("Successfully Connected to OnlineJeopardy DB.")
sqlite_insert_query = """INSERT INTO users
(User_id, Name, Score, Last_time)
VALUES
(?, ?, ?, ?)"""
add_to_db = cursor.execute(sqlite_insert_query, (user_id, name, score, last_time))
conn.commit()
cursor.close()
except sqlite3.Error as error:
print("Failed to insert data into sqlite table: ", error)
finally:
if (conn):
conn.close()
print("The SQLite connection is closed")
When I execute this query in DB Browser, with actual values instead of placeholders, it all goes well.
I've tried swapping placeholders to actual values (as below) within the query in sqlite3 but the outcome was the same.
conn = sqlite3.connect('OnlineJeopardy.db')
cursor = conn.cursor()
print("Successfully Connected to OnlineJeopardy DB.")
sqlite_insert_query = """INSERT INTO users
(User_id, Name, Score, Last_time)
VALUES
('ID-1337', 'Adam', 20, '2020-06-12 23:18:58')"""
add_to_db = cursor.execute(sqlite_insert_query)
conn.commit()
cursor.close()
I try to use sqlite3 to import data to a table babyName in my AWS RDS database. For the two methods I tried, the first one data_entry() works fine every time but the second new_data_entry() gave me
Cursor is not connected
or
Not all parameters are used
error. Could you please help me?
import mysql.connector
from mysql.connector import errorcode
# start connection
try:
cnn = mysql.connector.connect(
user = '*****',
password = '*****',
host = '*****-mysql.*****.us-****-1.rds.amazonaws.com',
database = '*******')
print('It works!')
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print('Somethign is wrong with username or password')
elif e.errno == errorcode.ER_BAD_DB_ERROR:
print('Database does not exist')
else:
print(e)
# start cursor
import sqlite3
c = cnn.cursor()
def creat_table():
c.execute("CREATE TABLE IF NOT EXISTS babyName (name TEXT, gender TEXT, frequency INTEGER, year TEXT)")
def data_entry():
c.execute("INSERT INTO babyName VALUES ('Mary', 'F', 1234, '2008')")
cnn.commit()
c.close()
cnn.close()
def new_data_entry():
name = 'Wendy'
gender = 'F'
frequency = 321
year = '2006'
c.execute("INSERT INTO babyName (name, gender, frequency, year) VALUES (?, ?, ?, ?)", (name, gender, frequency, year))
cnn.commit()
c.close()
cnn.close()
# creat_table()
data_entry()
print('It works!')
new_data_entry()
The error message I kept getting:
It works!
It works!
Traceback (most recent call last):
File "/Users/*****/sqlite3_python_cc.py", line 53, in <module>
new_data_entry()
File "/Users/*****/sqlite3_python_cc.py", line 45, in new_data_entry
c.execute("INSERT INTO babyName (name, gender, frequency, year) VALUES (?, ?, ?, ?)", values)
File "/Users/*****/anaconda/lib/python3.6/site-packages/mysql/connector/cursor.py", line 529, in execute
raise errors.ProgrammingError("Cursor is not connected")
mysql.connector.errors.ProgrammingError: Cursor is not connected
At the end of data_entry you have closed the connection to the database, cnn, which is saved as a variable in the global scope. When you attempt to run new_data_entry, the connection has already been closed, which is what is give you the error.
Instead, leave the connection open until you are finished.
import sqlite3
c = cnn.cursor()
def creat_table():
c.execute("CREATE TABLE IF NOT EXISTS babyName (name TEXT, gender TEXT, frequency INTEGER, year TEXT)")
def data_entry():
c.execute("INSERT INTO babyName VALUES ('Mary', 'F', 1234, '2008')")
cnn.commit()
def new_data_entry():
name = 'Wendy'
gender = 'F'
frequency = 321
year = '2006'
c.execute("INSERT INTO babyName (name, gender, frequency, year) VALUES (?, ?, ?, ?)", (name, gender, frequency, year))
cnn.commit()
def finish():
c.close()
cnn.close()
data_entry()
print('It works!')
new_data_entry()
finish()
I have the problem solved!
def new_data_entry():
name = 'Wendy'
gender = 'F'
frequency = 321
year = '2006'
c.execute("INSERT INTO babyName (name, gender, frequency, year) VALUES (%s, %s, %s, %s)", (name, gender, frequency, year))
cnn.commit()
def finish():
c.close()
cnn.close()
Change all the "?" to "%s" and the codes all run through~ Thank you guys!
Getting an error when trying to insert a row into SQLite table from Python.
The relevant code;
sql = '''INSERT INTO scr(scr=?, close_date=?, vendor=?, application=?, dev=?, tester=?, release=?)''', (issueId, closeDate, vendor, application, assignedDev, assignedTester, enterpriseRelease)
try:
cursor.execute(sql)
db.commit()
except Exception, err:
print("\nFailed to insert row into table scr:\n" + str(sql))
print(Exception, err)
and the error returned:
Failed to insert row into table scr:
('INSERT INTO scr(scr=?, close_date=?, vendor=?, application=?, dev=?, tester=?, release=?)', ('236500', '0', 'Database', 'Starbase Deleted', 'john.doe', 'jane.doe', 'None'))
(<type 'exceptions.Exception'>, ValueError('operation parameter must be str or unicode',))
Your sql statement is not right, try this:
sql = '''INSERT INTO scr(scr, close_date, vendor, application, dev, tester, release) VALUES (?, ?, ?, ?, ?, ?, ?)'''
params = (issueId, closeDate, vendor, application, assignedDev, assignedTester, enterpriseRelease)
try:
cursor.execute(sql, params)
db.commit()
except Exception as err:
print("\nFailed to insert row into table scr:\n" + str(sql))
print(Exception, err)