0

I'm getting different results with what I understand to be the same query from two different interfaces. The first is a mysql shell:

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

The second is a little function I put together to test a query that would pull a certain amount of records based on the datetime field "sub_date":

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

Here's the python module:

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
1
  • @Travesty3 Make that an answer and I'll vote for it :) Commented Nov 19, 2012 at 15:54

1 Answer 1

4

I know nearly nothing about python. But I'm pretty sure you need to put extra quotes around your date parameter in order for it to be quoted in the query string. Probably more like:

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

(note the extra quotes around the second %s).

Sign up to request clarification or add additional context in comments.

2 Comments

That was it. Thank you very much for the quick and easy answer.
No prob. Gotta love it when it's a simple fix :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.