使用python时Mysql限制偏移错误

2024-09-29 08:17:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究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)

错误:

^{pr2}$

有人能告诉我上面的问题是什么吗, 实际上,我试图用python实现分页网页.py框架


Tags: 数据import数据库asconnectmysqlpageconn
2条回答

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

限制和偏移都没有使用=等于符号,所以您的SQL确实不正确。在

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

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

请注意参数的顺序;LIMIT排在第一位,所以先传入perpage参数。在

您只需使用:

^{2}$

同样,将OFFSET替换为逗号,交换参数。在

相关问题 更多 >