How to Count the Number of Rows in a MySQL Table in Python? Last Updated : 12 Nov, 2020 Comments Improve Suggest changes Like Article Like Report MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server we use various modules in Python such as PyMySQL, mysql.connector, etc. In this article, we are going to get the number of rows in a specific MySQL table in a Database. First, we are going to connect to a database having a MySQL table. The SQL query that is going to be used is: SELECT * FROM table-name And finally, display the number of rows in the table. Below are some programs which depict how to count the number of rows from a MySQL table in a Database: Example 1 Below is the table geeksdemo in database geek which is going to be accessed by a Python script: Below is the program to get the number of rows in a MySQL table: Python3 # import required modules import pymysql pymysql.install_as_MySQLdb() import MySQLdb # connect python with mysql with your hostname, # username, password and database db= MySQLdb.connect("localhost", "root", "", "geek") # get cursor object cursor= db.cursor() # get number of rows in a table and give your table # name in the query number_of_rows = cursor.execute("SELECT * FROM geeksdemo") # print the number of rows print(number_of_rows) Output: Example 2: Here is another example to get the number of rows from a table in a given database, below is the table scheme and rows: Below is the python script to get row count from the table TechCompanies: Python3 # import required modules import pymysql pymysql.install_as_MySQLdb() import MySQLdb # connect python with mysql with your hostname, # username, password and database db= MySQLdb.connect("localhost", "root", "", "techgeeks") # get cursor object cursor= db.cursor() # get number of rows in a table and give your table # name in the query number_of_rows = cursor.execute("SELECT * FROM techcompanies") # print the number of rows print(number_of_rows) Output: Comment More infoAdvertise with us Next Article How to Count the Number of Rows in a MySQL Table in Python? A anurag702 Follow Improve Article Tags : Python Python-mySQL Practice Tags : python Similar Reads How to count the number of lines in a CSV file in Python? Counting the number of lines in a CSV file in Python means determining how many rows the file contains, including or excluding the header depending on your needs. For example, if your CSV file has 100 data rows plus one header row, the total line count is 101. We will use the following dataset to de 2 min read How to Count the Number of Rows of a Given SQLite Table using Python? In this article, we will discuss how we can count the number of rows of a given SQLite Table using Python. We will be using the cursor_obj.fetchall() method to do the same. This method fetches all the rows of a query result. It returns all the rows as a list of tuples. An empty list is returned if t 2 min read How to count the number of pages in a PDF file in Python In this article, we will see how can we count the total number of pages in a PDF file in Python, For this article there is no such prerequisite, we will use PyPDF2 library for this purpose. PyPDF2 is a free and open-source pure-Python PyPDF library capable of performing many tasks like splitting, me 4 min read How to Print Out All Rows of a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Â In order to access MySQL databases from a 2 min read How to Add a Column to a MySQL Table in Python? Prerequisite: Python: MySQL Create Table Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. Â ALTER statemen 4 min read Count number of lines in a text file in Python Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output: 3 min read How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python? MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a w 2 min read How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module. 3 min read How to Get the Minimum and maximum Value of a Column of a MySQL Table Using Python? Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the Minimum and Maximum Value of a Column of a MySQL Table Using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database 2 min read How to Show All Tables in MySQL using Python? A connector is employed when we have to use mysql with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. In order to make python interact with 1 min read Like