How to Get a Specified Number of Records in Python MySQL


In this example we will show how to get a specified number of records from a specified location in Python MySQL.

Source Code

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    port=3306,
    user="yourusername",
    password="yourpassword",
    db="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM us_state LIMIT 6 OFFSET 1"

mycursor.execute(sql)

results = mycursor.fetchall()

for row in results:
    print(row)

mydb.close()

Output:

(1, 'Alaska', 'AK', 'Juneau', 665384)
(2, 'Arizona', 'AZ', 'Phoenix', 113990)
(3, 'Arkansas', 'AR', 'Little Rock', 53179)
(4, 'California', 'CA', 'Sacramento', 163695)
(5, 'Colorado', 'CO', 'Denver', 104094)
(6, 'Connecticut', 'CT', 'Hartford', 5543)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments