Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26648

Mysql LIMIT OFFSET error using python

I am working on python and tried to fetch some data from Mysql Database and following is the query

import MySQLdb as mdb
page  = 1
perpage = 3
offset = (int(page) - 1) * perpage
conn = mdb.connect(user='root', passwd='redhat', db='Python_Web', host='localhost')
cursor_posts = conn.cursor()
posts = "select * from projects LIMIT = %s OFFSET = %s " %(offset,perpage)
cursor_posts.execute(posts)

Error:

ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/IPython/ultraTB.py", line 667, in text
    locals,formatvalue=var_repr))
  File "/usr/lib64/python2.7/inspect.py", line 885, in formatargvalues
    specs.append(strseq(args[i], convert, join))
  File "/usr/lib64/python2.7/inspect.py", line 840, in strseq
    return convert(object)
  File "/usr/lib64/python2.7/inspect.py", line 882, in convert
    return formatarg(name) + formatvalue(locals[name])
KeyError: 'connection'

IPython's exception reporting continues...

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)

/home/local/user/python_webcode/<ipython console> in <module>()

/usr/lib64/python2.7/site-packages/MySQLdb/cursors.pyc in execute(self, query, args)
    172             del tb
    173             self.messages.append((exc, value))
--> 174             self.errorhandler(self, exc, value)
    175         self._executed = query
    176         if not self._defer_warnings: self._warning_check()

/usr/lib64/python2.7/site-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
     34     del cursor
     35     del connection
---> 36     raise errorclass, errorvalue
     37 
     38 re_numeric_part = re.compile(r"^(\d+)")

ProgrammingError: (1064, "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 '= 0 OFFSET = 3' at line 1")

Can anyone let me know whats wrong in my query above, actually i am trying to achieve pagination in python using web.py framework

Upvotes: 1

Views: 4439

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121396

Neither LIMIT nor OFFSET use = equals signs, so your SQL is indeed incorrect.

You really should use SQL parameters, where the database library quotes your values and prevents SQL injection attacks:

posts = "select * from projects LIMIT %s OFFSET %s"
cursor_posts.execute(posts, (perpage, offset))

Note the order of the parameters as well; LIMIT comes first, so pass in your perpage parameter first.

You can just use:

LIMIT %s, %s
posts = "select * from projects LIMIT %s, %s"
cursor_posts.execute(posts, (offset, perpage))

as well, replacing OFFSET with a comma, and swapping the arguments.

Upvotes: 3

Brian
Brian

Reputation: 161

I belive this should be just LIMIT %s OFFSET %s. No equal signs.

Upvotes: 6

Related Questions