使用python的Mysql限制偏移错误

使用python的Mysql限制偏移错误,python,mysql,pagination,limit,Python,Mysql,Pagination,Limit,我正在使用python,试图从Mysql数据库中获取一些数据,下面是查询 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 * fro

我正在使用python,试图从Mysql数据库中获取一些数据,下面是查询

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: 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")
有人能告诉我上面的问题是什么吗,
实际上,我正在尝试使用web.py framework在python中实现分页

我相信这应该只是限制%s偏移量%s。没有等号。

我认为这应该只是限制%s偏移量%s。没有等号。

既不限制也不偏移使用=等号,因此您的SQL确实不正确

您确实应该使用SQL参数,其中数据库库引用您的值并防止SQL注入攻击:

posts = "select * from projects LIMIT %s OFFSET %s"
cursor_posts.execute(posts, (perpage, offset))
注意参数的顺序;限制是第一位的,所以首先传入perpage参数

您只需使用:

限制%s,%s posts=从项目中选择*限制%s,%s 游标\u posts.executeposts,偏移量,每页
另外,用逗号替换偏移量,并交换参数。

限制和偏移量都不使用等号,因此您的SQL确实不正确

您确实应该使用SQL参数,其中数据库库引用您的值并防止SQL注入攻击:

posts = "select * from projects LIMIT %s OFFSET %s"
cursor_posts.execute(posts, (perpage, offset))
注意参数的顺序;限制是第一位的,所以首先传入perpage参数

您只需使用:

限制%s,%s posts=从项目中选择*限制%s,%s 游标\u posts.executeposts,偏移量,每页
另外,用逗号替换偏移量,并交换参数。

是的,注释已删除,向下滚动的距离不够。是的,注释已删除,向下滚动的距离不够。@random\u stackoverflow\u用户:OP的参数颠倒了,我没有特别注意。对不起,修改了我以前的注释。我猜您已经反转了offset和perpage参数以反映OP的参数。相反,您可以只使用语法限制%s,%s,其中第一个参数是开始的有序记录偏移量,第二个是要检索的记录的偏移量perpage@random_stackoverflow_user:我知道我应该检查一下文件-P@random_stackoverflow_user:OP的参数被颠倒了,我没有特别注意。对不起,修改了我之前的评论。我猜您已经反转了offset和perpage参数以反映OP的参数。相反,您可以只使用语法限制%s,%s,其中第一个参数是开始的有序记录偏移量,第二个是要检索的记录的偏移量perpage@random_stackoverflow_user:我知道我应该检查一下文件-P