2014-03-05 72 views
1

所以我的問題是這樣一個事實,當我嘗試和運行一個查詢,這個特定的一個:查詢工作在MySQL工作臺,但不是在MySQL連接器

SELECT call_start_time, call_end_time, sid FROM calls WHERE (call_start_time BETWEEN 
'2014-01-08 00:00:00' AND '2014-01-09 00:00:00') AND voice_info_id IS NOT NULL AND 
call_start_time IS NOT NULL AND call_end_time IS NOT NULL 

它運行在工作臺上,並返回正確的數據我想要,但是當我嘗試使用MySQL連接器和Python在下面的代碼中運行查詢時,它會抱怨說即使它正確,語法也是不正確的。我仔細查看了連接器文檔,找不到任何與我正在做的事情和連接器文檔上的使用連接器/ Python查詢數據的示例之間的差異。我使用的代碼如下:

from time import * 
import numpy as np 
import matplotlib.pyplot as plt 
import mysql.connector 
from datetime import datetime 
from sets import Set 

config = { 
    'user': 'xxxxxxxxx', 
    'password': 'xxxxxxxxx', 
    'host': '127.0.0.1', 
    'database': 'xxxxxxxxx', 
    'raise_on_warnings': True, 
} 

tick = set([]) 
epoch_start_call_set = set([]) 
epoch_end_call_set = set([]) 
from mysql.connector import errorcode 
try: 
    cnx = mysql.connector.connect(**config) 
    cursor = cnx.cursor() 
    print("DATABASE CONNECTION ESTABLISHED") 
    print 

    userIn = raw_input("Type Start Date (MM-DD-YYYY): ") 
    userEnd = raw_input("Type End Date (MM-DD-YYYY): ") 
    startdate = datetime.strptime(userIn, '%m-%d-%Y') 
    enddate = datetime.strptime(userEnd, '%m-%d-%Y') 
    epoch_s = mktime(startdate.timetuple()) 
    epoch_e = mktime(enddate.timetuple()) 
    while epoch_s <= epoch_e: 
     current_tick = epoch_s + 60 
     epoch_s += 60 
     tick.add(current_tick) 

    query = "SELECT call_start_time, call_end_time, sid FROM calls" \ 
      "WHERE call_start_time BETWEEN %s AND %s " \ 
      "AND voice_info_id IS NOT NULL AND call_start_time IS NOT NULL " \ 
      "AND call_end_time IS NOT NULL" 
    cursor.execute(query, (startdate, enddate)) 


except mysql.connector.Error as err: 
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: 
     print("Your password or username is incorrect, please try again") 
    elif err.errno == errorcode.ER_BAD_DB_ERROR: 
     print("Your database does not exist, please try again") 
    else: 
     print(err) 
else: 
    cnx.close() 

,我正在尋找通過數據是在這裏工作臺上,有很多,但是這是我測試的樣本。 http://i.imgur.com/LzQn6py.png

這是當我運行工作臺的查詢結果: http://i.imgur.com/HFeCp9y.png

這是我想要的結果。我的查詢失敗有什麼特別原因嗎?我試過重寫和手動替換查詢中的確切日期,而不是使用參數注入,但似乎沒有解決它。

回答

2

錯別字:

query = "SELECT call_start_time, call_end_time, sid FROM calls" \ 
                   ^--- 
     "WHERE call_start_time BETWEEN %s AND %s " \ 
     ^--- 

你在指定的現場缺少空間,產生:

SELECT ... FROM callsWHERE 
        ^--- 
+0

哇,這是愚蠢的。我應該更加小心我的線條包裝! – Jreed