This tutorial shows you how to query data from a MySQL database in Python by using MySQL Connector/Python API such as fetchone()
, fetchmany()
, and fetchall()
.
To query data in a MySQL database from Python, you need to do the following steps:
We will show you how to use fetchone()
, fetchmany()
, and fetchall()
methods in more detail in the following sections.
The fetchone()
method returns the next row of a query result set or None
in case there is no row left. Let’s take a look at the following code:
Let’s examine the code in detail:
In case the number of rows in the table is small, you can use the fetchall()
method to fetch all rows from the database table. See the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 from mysql.connector import MySQLConnection, Error from python_mysql_dbconfig import read_db_config def query_with_fetchall(): try: dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() cursor.execute("SELECT * FROM books") rows = cursor.fetchall() print('Total Row(s):', cursor.rowcount) for row in rows: print(row) except Error as e: print(e) finally: cursor.close() conn.close() if __name__ == '__main__': query_with_fetchall()
The logic is similar to the example with the fetchone()
method except for the fetchall()
method call part. Because we fetched all rows from the books table into the memory, we can get the total rows returned by using the rowcount
property of the cursor object.
For a relatively big table, it takes time to fetch all rows and return the result set. In addition, fetchall()
needs to allocate enough memory to store the entire result set in the memory. This is inefficient and not a good practice.
MySQL Connector/Python provides us with the fetchmany()
method that returns the next number of rows (n) of the result set, which allows us to balance between time and memory space. Let’s take a look at how do we use fetchmany()
method.
First, we develop a generator that chunks the database calls into a series of fetchmany()
calls as follows:
Second, we can use the iter_row()
generator to fetch 10 rows at a time as shown below: