【问题标题】:Mysql LIMIT OFFSET error using python使用python的Mysql LIMIT OFFSET错误
【发布时间】:2012-10-06 08:34:52
【问题描述】:

我正在研究 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 框架在 python 中实现分页

【问题讨论】:

  • 是的,评论已被删除,向下滚动不够远。

标签: python mysql pagination limit


【解决方案1】:

LIMIT和OFFSET都没有使用=等号,所以你的SQL确实不正确。

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

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

还要注意参数的顺序; LIMIT 先出现,所以请先传入你的 perpage 参数。

你可以使用:

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

同样,用逗号替换OFFSET,并交换参数

【讨论】:

  • @random_stackoverflow_user:OP 的参数颠倒了,我没有特别注意。
  • 抱歉,修改了我之前的评论。我猜你已经反转了偏移量和 perpage 参数以反映 OP 的参数。相反,您可以只使用语法“LIMIT %s, %s”,其中第一个参数是开始的有序记录(偏移量),第二个是要检索的记录数(每页)
  • @random_stackoverflow_user:我知道我应该检查文档。 :-P
【解决方案2】:

我相信这应该只是LIMIT %s OFFSET %s。没有等号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 2020-08-19
    • 2020-12-30
    • 1970-01-01
    • 2020-12-08
    • 2011-05-16
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多