我正在尝试使用 pymysql 对 Mysql 数据库执行 SELECT 语句。
这是代码。我将一个变量传递给 select 语句,令我惊讶的是,这是一个巨大的痛苦。知道我在这里缺少什么吗?
def getUrlFromDatabase(n):
stmt = "SELECT * FROM jsonTes ORDER BY website LIMIT %s-1,1"
cur.execute(stmt,str(n))
return cur.fetchone()
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='passwd', db='email_database', charset='utf8')
cur = conn.cursor()
cur.execute("USE database")
getUrlFromDatabase(0)
Error:
这就是我试图实现的目标:返回 MySQL 查询的第 n 条记录 https://stackoverflow.com/questions/2224951/return-the-nth-record-from-mysql-query
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''0'-1,1' at line 1")
LIMIT
MySQL 中采用数字参数,它们必须都是非负整数常量。您必须在 Python 中计算表达式,然后将整数作为单个参数传递。另外,您需要将参数放入元组中:
def getUrlFromDatabase(n):
stmt = "SELECT * FROM jsonTes ORDER BY website LIMIT %s, 1"
cur.execute(stmt, (n-1 if n > 0 else 0,))
return cur.fetchone()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)