您可以使用 "LIMIT" 语句限制从查询返回的记录数:
选取 "customers" 表中的前五条记录:
import mysql.connectormydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM customers LIMIT 5")myresult = mycursor.fetchall()for x in myresult: print(x)
如果希望从第三条记录开始返回五条记录,您可以使用 "OFFSET" 关键字:
从位置 3 开始返回 5 条记录:
import mysql.connectormydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")myresult = mycursor.fetchall()for x in myresult: print(x)