2012-11-19 48 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