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
asked Nov 19, 2012 at 15:49
numb3rs1x
5,4116 gold badges35 silver badges46 bronze badges
-
@Travesty3 Make that an answer and I'll vote for it :)RocketDonkey– RocketDonkey2012年11月19日 15:54:26 +00:00Commented Nov 19, 2012 at 15:54
1 Answer 1
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).
answered Nov 19, 2012 at 15:56
Travesty3
14.5k7 gold badges64 silver badges103 bronze badges
default