Tuesday , March 19 2024
Python MySQL Limit

Python MySQL Limit

11 Python MySQL Limit

Python MySQL Limit

  • You can limit the number of records returned from the query, by using the “LIMIT” statement:
In [23]:
import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="itronix"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
    print(x)
(1, 'Karan', 'Detroit')
(4, 'Varun', 'Ontario')
(5, 'Priya', 'Delhi')
(6, 'Atharv', 'Goa')
(7, 'Sia', 'Dubai')

Start From Another Position

  • If you want to return five records, starting from the third record, you can use the “OFFSET” keyword:
In [24]:
import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="itronix"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5 OFFSET 2")
myresult = mycursor.fetchall()
for x in myresult:
    print(x)
(5, 'Priya', 'Delhi')
(6, 'Atharv', 'Goa')
(7, 'Sia', 'Dubai')

About Machine Learning

Check Also

Python MySQL Insert Into Table

Python MySQL Insert Into Table

4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in …

Leave a Reply

Your email address will not be published. Required fields are marked *