Limit
如果我们要设置查询的数据量,可以通过 "LIMIT" 语句来指定
demo_mysql_test.py
读取前 3 条记录:
importmysql.connectormydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites LIMIT 3")myresult = mycursor.fetchall()forxinmyresult: print(x)
执行代码,输出结果为:
(1,'RUNOOB','https://www.runoob.com')
(2,'Google','https://www.google.com')
(3,'Github','https://www.github.com')
也可以指定起始位置,使用的关键字是 OFFSET:
demo_mysql_test.py
从第二条开始读取前 3 条记录:
importmysql.connectormydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1") # 0 为 第一条,1 为第二条,以此类推myresult = mycursor.fetchall()forxinmyresult: print(x)
执行代码,输出结果为:
(2,'Google','https://www.google.com')
(3,'Github','https://www.github.com')
(4,'Taobao','https://www.taobao.com')