How to Limit a MySQL Query in Python


First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python .

How to Limit the Result Returned from MySQL in Python

import mysql.connector

mydb = mysql.connector.connect(
  host = "localhost",
  user = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 10")
myresult = mycursor.fetchall()

for x in myresult:
  print(x)

How to Start From Another Position in MySQL from Python

import mysql.connector

mydb = mysql.connector.connect(
  host = "localhost",
  user = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 10 OFFSET 5")
myresult = mycursor.fetchall()

for x in myresult:
  print(x)