Articles

Python MySQL LIMIT

Python MySQL LIMIT


In this Python tutorial, we will see how to select data in mysql database with LIMIT clause through python.

PYTHON MySQL LIMIT

 

In sql, LIMIT is used to return only particular number of rows from the top of the table.

if LIMIT is 2, then it will return only top 2 rows from the table in a database.

 

Syntax :


	
SELECT [*|columns]
FROM table_name
LIMIT value

where, value takes an integer value that return number of rows from top.

Now, let' see how to perform SELECT with LIMIT clause  in xampp server through python.

Before that make sure that you have created a database named - tutorialsinhand  and table inside it  market with the following records.

python mysql limit

Steps:

1. Import mysql.connector module
    Syntax:
    import mysql.connector

2. Create the connection object to connect with xampp server using the below credentials.
    Syntax:
    connection_obj = mysql.connector.connect(
                          host="localhost",
                          user="root",
                          password=""
                                                                        )
3. Create cursor object to execute the query.
    Syntax:
    connection_obj.cursor()

4. Write sql query to select columns by applying LIMIT clause from the table.
   
5. Fetch the resulted rows using fetchall().
    Syntax:
    cursor.fetchall()

6. Display using for loop 
     Syntax:
         for i in results:
            print(i)

Note - It will return the values in a tuple.

Example 1:

In this example, we are selecting all columns from market table and display only 2 rows.

SELECT * FROM market LIMIT 2

With Python Code:

#import the module
import mysql.connector

#create the connection by specifying the 
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="tutorialsinhand"
)

#creating cursor object
cursor_obj = connection_obj.cursor()


#select top 2 rows from all columns
cursor_obj.execute("SELECT * FROM market LIMIT 2")

#fetch the rows
results = cursor_obj.fetchall()

#get the values
for i in results:
  print(i)

Output:

('koti-cente', 'kakumanu', 8)
('bakery', 'hyderabad', 5)

We can see that only 2 rows are displayed.

Example 2:

In this example, we are selecting only rating column from market table and display only 1 row.

SELECT rating FROM market LIMIT 1

Python Code:

#import the module
import mysql.connector

#create the connection by specifying the 
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="tutorialsinhand"
)

#creating cursor object
cursor_obj = connection_obj.cursor()


#select top 1 row from raing column
cursor_obj.execute("SELECT rating FROM market LIMIT 1")

#fetch the rows
results = cursor_obj.fetchall()

#get the values
for i in results:
  print(i)

Output:

Returned only top value in rating column.

(8,)

 


Python MySql

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Sep 22,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!