Fatah
Fatah

Reputation: 1

Python SQL SELECT syntax with multiple parameters LIMIT

I have an SQL statement that works in MySQL:

SELECT * FROM kaio_jadwal WHERE param1=88 AND param2=973 AND param3=22554 LIMIT 0,10

When I try with Python:

sql = """SELECT * FROM kaio_jadwal WHERE param1=%s AND param2=%s AND param3=%s LIMIT %s OFFSET %s"""
filter = (param1,param2,param3,limit,offset)

And I got an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''10' OFFSET '25''

And this is my code:

sql = """SELECT * FROM kaio_jadwal WHERE param1=%s AND param2=%s AND param3=%s LIMIT %s OFFSET %s"""
filter = (param1,param2,param3,limit,offset)
cursor.execute(sql,filter)
rows = cursor.fetchall()

How should the syntax be in Python to work?

Upvotes: 0

Views: 1587

Answers (1)

Hrabal
Hrabal

Reputation: 2523

cursor.execute is converting your limit and offset into strings, but MySQL expects ints. Try changing your SQL string with the %d notation for offset and limit:

sql = """SELECT * FROM kaio_jadwal WHERE param1=%s AND param2=%s AND param3=%s LIMIT %d OFFSET %d"""

Upvotes: 2

Related Questions