Python无法将两次之间的时间更新到mySQL

2024-04-25 03:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表,有3列:ID; TIMEON; TIMEOFF; TIMES和数据

22 | 2016-05-25 19:17:11 | 2016-05-26 18:26:43 | 0000-00-00 00:00:00

#!/usr/bin/env python

import time
import MySQLdb
import RPi.GPIO as GPIO
import datetime
from datetime import datetime
from time import localtime, strftime, strptime

db = MySQLdb.connect(host="localhost", user="123", passwd="tesst", db="raspi")
cur= db.cursor()
cur.execute("SELECT TIMEON, TIMEOFF FROM DP WHERE Id = 22")
time_results = cur.fetchall()
for row in time_results:
    tgianON = row[0]
    tgianOFF = row[1]
aaa = abs(tgianOFF - tgianON)
print aaa
cur.execute("UPDATE DP SET TIMEUSE=%s WHERE Id = %s",(aaa, 22))
db.commit()
cur.close()
db.close()
  • 我想计算TIMEONTIMEOFF之间的时间,然后将其插入到当前行中的mysql(TIMES在我的表中),但运行时出错:
  • 它可以打印时间,但不能插入数据

22:46:54

tet.py:17: Warning: Out of range value for column 'TIMEUSE' at row 1 cur.execute("UPDATE DP SET TIMEUSE=%s WHERE Id = %s",(aaa, 22))

我认为aaa的格式是错误的。对吗?你知道吗


Tags: importidexecutedbdatetimetimewhererow