Python MySQL- limiet
Beperk die resultaat
U kan die aantal rekords wat van die navraag teruggestuur word beperk deur die "LIMIT"-stelling te gebruik:
Voorbeeld
Kies die 5 eerste rekords in die "kliënte"-tabel:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5")
myresult = mycursor.fetchall()
for x in
myresult:
print(x)
Begin vanuit 'n ander posisie
As jy vyf rekords wil teruggee, vanaf die derde rekord, kan jy die "OFFSET"-sleutelwoord gebruik:
Voorbeeld
Begin vanaf posisie 3 en gee 5 rekords terug:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor =
mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5
OFFSET 2")
myresult = mycursor.fetchall()
for x in
myresult:
print(x)