1292,截断了错误的时间值:(MySQL和Python)

时间:2019-12-11 11:37:29

标签: python mysql

我们已经开发了一个小程序,我们想要获取一个值,并同时在MySQL数据库中搜索值相同的其他巧合。为此,我们正在比较时间,并使用以下SQL查询:

value = pd.read_sql('SELECT third_temp_lpn FROM Raw_Validated WHERE TIME(time_end) = TIME("%s") AND third_temp_lpn = "%s"' % (x + datetime.timedelta(minutes=20), list_lpn_temp[-1]), conn).astype(float).values

在这里,我们使用TIME(time_end),在我们的MySQL数据库上使用TIME(time_end)时效果很好。该查询应该将时间与日期时间隔离开,我们将其设置为等于TIME(%s),这是我们在python中的日期时间(请参阅完整代码)。

在python中执行上述查询时,我们收到一个奇怪的错误,如下所示:

Warning: (1292, "Truncated incorrect time value: '2019-08-31'")
    cursor.execute(statement, parameters)

这里有谁可以帮助您?

代码

import pandas as pd
import numpy as np
import pprint
import datetime
import math
from pandas.io import sql
from sqlalchemy import create_engine

list_lpn_temp = []
times = []
values = []

engine = create_engine("mysql://root:DTULab@123@localhost/Afgangsprojekt")
conn = engine.connect()

firstTime = pd.read_sql('SELECT MIN(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()
firstTime = (pd.to_datetime(firstTime[0])-datetime.timedelta(minutes=10)).round('20T')

lastTime = pd.read_sql('SELECT MAX(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()
lastTime = (pd.to_datetime(lastTime[0])-datetime.timedelta(minutes=10)).round('20T')

print (firstTime[0])
print (lastTime[0])

index = pd.date_range(start=firstTime.min(), end=lastTime.max(), freq='20T')

for x in index:

    a_temp = pd.read_sql('SELECT temperature FROM Raw_Data WHERE topic = "lpn1" AND timestamp > "%s" AND timestamp < "%s" ORDER BY timestamp DESC LIMIT 1' % (x, x+datetime.timedelta(minutes=20)), conn).astype(float).values
    a_temp = a_temp.astype(int)

    if a_temp:
        list_lpn_temp.extend(a_temp[0])
    else:
        print (x, " | ", x + datetime.timedelta(minutes=20))
        value = pd.read_sql('SELECT third_temp_lpn FROM Raw_Validated WHERE TIME(time_end) = TIME("%s") AND third_temp_lpn = "%s"' % (x + datetime.timedelta(minutes=20), list_lpn_temp[-1]), conn).astype(float).values
        time = pd.read_sql('SELECT time_end FROM Raw_Validated WHERE TIME(time_end) = TIME("%s") AND third_temp_lpn = "%s"' % (x + datetime.timedelta(minutes=20), list_lpn_temp[-1]), conn).astype(str).values
        values.append(value)
        times.append(time)
        print (values[0])
        print (times[0])
        list_lpn_temp.append(float('nan'))

print (list_lpn_temp)

0 个答案:

没有答案