Python 2.7の2つの日付の時差を取得したいと思います。からに日付が格納されています。次のコードを実行2つの日付間の差分の計算MySQL/Python
:
import datetime
import time
from time import sleep
import MySQLdb as mdb
connection = mdb.connect('localhost', 'root', 'pwd', 'mydatabase');
cursor = connection.cursor()
TimeFormat = '%Y-%m-%d %H:%M:%S'
#Insert times and type of event into database
with connection:
#First event
Now=datetime.datetime.now()
Timewhen1=Now.strftime(TimeFormat)
print "Start time", Timewhen1
Type="1"
cursor.execute('INSERT INTO LogEvent (Timewhen, Type) VALUES (%s, %s)',(Timewhen1,Type))
sleep(1) #Real time will be unknown, seconds to days
#Second event
Now=datetime.datetime.now()
Timewhen2=Now.strftime(TimeFormat)
print "Stop time", Timewhen2
Type="0"
cursor.execute('INSERT INTO LogEvent (Timewhen, Type) VALUES (%s, %s)',(Timewhen2,Type))
#Get time difference
with connection:
cursor.execute("SELECT Timewhen FROM LogEvent ORDER BY ID DESC LIMIT 0,1")
result=cursor.fetchone()
cursor.execute("SELECT Timewhen FROM LogEvent ORDER BY ID DESC LIMIT 1,1")
result2=cursor.fetchone()
diff=result2-result
print "Diff", diff
は、以下の結果を得た:
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
結果/結果2は(datetime.datetime(2017, 1, 27, 22, 25, 39),)
の形式です。
私はタプル/文字列形式で何か間違っています。どんな助けもありがとう!