I have the following query that running in SQLyog
SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-09 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);
If I run the same query in Python or Django, I get this message:
Warning: Truncated incorrect datetime value: '2012-09-09 00:00:00-05:00'
I’ve printed out the query from python to ensure it’s the same as above. It is identical (including the ending semicolon). I suspect that it’s having issues with the timezone offset, but I don’t know why it’d work in SQLyog then.
The query that is printed is:
--------------------------------------------------
SELECT * FROM sb_bans WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-09 00:00:00-05:00','%Y-%m-%d %H:%i:%s') - INTERVAL 1 DAY);
--------------------------------------------------
The Django code used to run this query is:
query = """SELECT * FROM sb_bans WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-09 00:00:00-05:00','%Y-%m-%d %H:%i:%s') - INTERVAL 1 DAY);"""
myCursor = connections[system_db].cursor()
results = myCursor.execute(query) # Dies on this statement
resultcount = results.fetchall()
Why does this query work when I run it directly, but it fails when executed by my code?
There is a difference between your two queries:
First:
Django code:
One has a
%k, the other a%H. Perhaps that’s the root of your problem?