2015-04-01 151 views
2

我是一個完整的初學者,我有一個測試mysql數據庫設置。我目前正在嘗試檢查數據庫中列出的備份開始時間是否在當天的下午12點之前。比較python datetime到mysql datetime

UPDATE Clients SET backup_started=NOW() WHERE id=1; 

什麼我目前正在爲:時間使用下進入表

now = datetime.datetime.now() 
today12am = now.replace(hour=0, minute=0, second =0,) 
dbu.cursor.execute('SELECT id, company_name, backup_started,\ 
backup_finished FROM Clients WHERE backup_started>' + today12am) 
data = dbu.cursor.fetchone() 
print (data) 

我明白,這是試圖一個datetime.datetime比較字符串,這是哪裏有問題。我的問題是完成這個的最好方法是什麼?

錯誤:

TypeError: cannot concatenate 'str' and 'datetime.datetime' objects 

回答

2

讓你查詢參數

dbu.cursor.execute(""" 
    SELECT 
     id, company_name, backup_started, backup_finished 
    FROM 
     Clients 
    WHERE 
     backup_started > %s""", (today12pm,)) 
1

使用strftime

dbu.cursor.execute('SELECT id, company_name, backup_started,\ 
backup_finished FROM Clients WHERE backup_started> %s' %today12pm.strftime('%Y-%m-%d %H:%M:%S')) 

檢查該格式是正確的