9951 explained code solutions for 126 technologies


python-mysqlHow do I use Python to count the number of records in a MySQL database?


To count the number of records in a MySQL database using Python, you can use the cursor.execute() method of the MySQLdb library. This method takes in a SQL query as a parameter and returns the number of records in the database. The following example code block shows how to use this method to count the number of records in a table called myTable:

import MySQLdb

# establish connection to MySQL database
db = MySQLdb.connect(host="localhost", user="user", passwd="password", db="myDatabase")

# create a cursor object
cursor = db.cursor()

# execute a SQL query
cursor.execute("SELECT COUNT(*) FROM myTable")

# get the number of records
numRecords = cursor.fetchone()[0]

# print the number of records
print(numRecords)

The output of the above code would be the number of records in the myTable table.

The code can be broken down as follows:

  1. import MySQLdb: imports the MySQLdb library which provides access to MySQL databases.
  2. db = MySQLdb.connect(host="localhost", user="user", passwd="password", db="myDatabase"): establishes a connection to the MySQL database.
  3. cursor = db.cursor(): creates a cursor object which is used to execute SQL queries.
  4. cursor.execute("SELECT COUNT(*) FROM myTable"): executes a SQL query to count the number of records in the myTable table.
  5. numRecords = cursor.fetchone()[0]: fetches the number of records from the result of the SQL query.
  6. print(numRecords): prints the number of records.

For more information, please refer to the MySQLdb documentation.

Edit this code on GitHub