dimanche 26 juillet 2015

How do I compare dates and delete old rows with Python in a sqlite database?

I'm trying to check the rows in a sqlite database with Python, see if the UTC entry in the column called "time" is a certain age or greater, and if so, delete the row. There's got to be a simple way to do this, but I don't know exactly what this operation would even be called.

I keep getting an error: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'tuple'

Here's the code I'm working with:

d1 = datetime.datetime.now()

def DBcleaner():

conn = sqlite3.connect("headlines.sqlite")

c = conn.cursor()

timeCall = c.execute("SELECT * FROM headlines WHERE time < ?", (d1,))
result = c.fetchall()
for newTime in result:
    # difference = d1 - newTime
    if (d1 - newTime) > timedelta(minutes = 500):
        c.execute("DELETE FROM headlines WHERE time=?)", (newTime,))

Aucun commentaire:

Enregistrer un commentaire