2012-11-19 47 views
0

我得到了不同的结果,我知道它是来自两个不同接口的相同查询。第一个是mysql外壳:mysql查询在mysql-python中获得不同结果

mysql> select * from table where sub_date > '2012-11-08' order by sub_date asc limit 1\G 
*************************** 1. row *************************** 
       id: **176041922** 

第二个是一个小功能,我放在一起测试的查询,将拉动基础上的日期时间字段“sub_date”一定量的记录:

>>> r_query('>', '2012-11-08', '1') 
((**18393664L**, 3, .....) 

这里是python模块:

import MySQLdb 
myuser = MySQLdb.connect(host='localhost', user='myuser', passwd='mypass', db='mydatabase') 
cur = myuser.cursor() 

def r_query(oper, date, limit): 
    cur.execute("""select * from table where sub_date %s %s order by sub_date asc limit %s""" % (oper, date, limit)) 
    result = cur.fetchall() 
    print result 
+0

@ Travesty3做出答案,我会投它:) – RocketDonkey

回答

4

我对python几乎一无所知。但是我非常肯定你需要在你的日期参数中加上额外的引号,以便在查询字符串中引用它。可能更像:

cur.execute("""select * from table where sub_date %s '%s' order by sub_date asc limit %s""" % (oper, date, limit)) 

(注意第二个%s周围的额外引号)。

+0

就是这样。非常感谢您提供快速简单的答案。 – numb3rs1x

+0

没有问题。当它是一个简单的解决方案时,爱得到它:-) – Travesty3