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
这是我想要的结果。我的查询失败有什么特别原因吗?我试过重写和手动替换查询中的确切日期,而不是使用参数注入,但似乎没有解决它。
哇,这是愚蠢的。我应该更加小心我的线条包装! – Jreed