Home > Software design >  parameters are of unsupported type
parameters are of unsupported type

Time:09-27

I want to delete all rows in my table where the date is before or the same day as today, to update reservations in a hotel database.

dat_danas = datetime.datetime.today().date()
dat_danas.strftime("%d-%M-%Y")

conn = sqlite3.connect("rezervacije.db")
cursor = conn.cursor()

query = "DELETE FROM infoGosti WHERE DATE(odl_fld) <= ?"
cursor.execute(query, dat_danas,)
conn.commit()

CodePudding user response:

Try something like this example:

import sqlite3, datetime

""" creating connection & table """
db = sqlite3.connect(':memory:') # creates db in Memory
cursor = db.cursor()
cursor.execute('''CREATE TABLE store (id INTEGER PRIMARY KEY, name TEXT, validity DATE)''')
db.commit()

""" populating data """
stores = [
    ['Stock 1', '12.10.2021'],
    ['Stock 2', '17.04.2022'],
    ['Stock 3', '27.11.2022'],
    ['Stock 4', '23.09.2022'],
]
cursor.executemany('''INSERT INTO store (name, validity) VALUES (?,?)''', stores)
db.commit()

""" queries """
today = datetime.date.today().strftime('%d.%m.%Y')
cursor.execute(""" SELECT * FROM store """)    # result before deletion
db.commit()
res = cursor.fetchall()

query = """ DELETE FROM store WHERE validity < ? """
cursor.execute(query, (today,))
cursor.execute(""" SELECT * FROM store """)    # result after deletion
db.commit()
res = cursor.fetchall()
  • Related