3

I'm a complete beginner, I have a test mysql db set up. I am currently trying to check if the backup start time listed in the database is before 12pm on the current day. The time was entered into the table using the following:

UPDATE Clients SET backup_started=NOW() WHERE id=1;

What I am currently trying is:

now = datetime.datetime.now()
today12am = now.replace(hour=0, minute=0, second =0,)
dbu.cursor.execute('SELECT id, company_name, backup_started,\
backup_finished FROM Clients WHERE backup_started>' + today12am)
data = dbu.cursor.fetchone()
print (data)

I understand that it is trying to compare a datetime.datetime to a string and this is where it is having problems. My question is what is the best way to accomplish this?

error:

TypeError: cannot concatenate 'str' and 'datetime.datetime' objects

2 Answers 2

2

Make your query parameterized:

dbu.cursor.execute("""
    SELECT 
        id, company_name, backup_started, backup_finished 
    FROM 
        Clients 
    WHERE 
        backup_started > %s""", (today12pm, ))
Sign up to request clarification or add additional context in comments.

Comments

1

use strftime

dbu.cursor.execute('SELECT id, company_name, backup_started,\
backup_finished FROM Clients WHERE backup_started> %s' %today12pm.strftime('%Y-%m-%d %H:%M:%S'))

check that the format is correct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.