我正在将'2012-09-10 00:00:00-05:00'
传递给MySQL查询。使用Python的pytz module检索此值。
import pytz
class MyClass():
def __init____(self):
self.tz = pytz.timezone(settings.TIME_ZONE)
todaystart = self.tz.localize(datetime.now(self.tz).replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None), is_dst=None).astimezone(self.tz)
在todaystart
替换为:
SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-10 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);
如果我直接执行此查询,它会按预期返回数据。如果我将此查询放入代码中,则会出现此错误:Warning: Truncated incorrect datetime value: '2012-09-09 00:00:00-05:00'
我使用的代码就是这个(在Django中):
query = """SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-10 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);"""
myCursor = connections[system_db].cursor()
results = myCursor.execute(query) # Dies on this statement
resultcount = results.fetchall()
我没有在str_to_date的MySQL文档中看到偏移format字符串。我宁愿保留该偏移量,因为为第三方系统返回数据并保持原位,我不必在返回和返回日期之间执行查询之间做任何逻辑。但是,我不认为它与偏移量有关,因为如果我直接运行它会起作用。
当我对MySQL运行查询时,我做错了什么会导致此Warning
出现?
答案 0 :(得分:0)
Marc B说得对。
直接执行时,您会收到警告,但可能没有注意到:
mysql> SELECT * FROM mytable WHERE created > UNIX_TIMESTAMP(STR_TO_DATE('2012-09-10 00:00:00-05:00','%Y-%m-%d %k:%i:%s') - INTERVAL 1 DAY);
Empty set, 3 warnings (0.00 sec)
mysql> show warnings;
+---------+------+----------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect datetime value: '2012-09-10 00:00:00-05:00' |
| Warning | 1292 | Truncated incorrect datetime value: '2012-09-10 00:00:00-05:00' |
| Warning | 1292 | Incorrect datetime value: '1347148800' for column 'created' at row 1 |
+---------+------+----------------------------------------------------------------------+
3 rows in set (0.00 sec)