The retrieval of data from a database is a fundamental part of using Python in software development. Today, we will focus on the SELECT statement, which enables you to efficiently retrieve data from a MySQL database. In this guide, you will learn how to read a record from a 'books' table using a simple SELECT statement.
Key Insights
You will learn how to retrieve data using a method, working with cursor objects, and how important it is to close the connection with the database.
Step-by-Step Guide
To read all records from a MySQL database, you will create the getbooks method. In this process, you will come into contact with cursors and execute a SELECT statement.
First, you need to ensure that you have a connection object that holds the connection to the database. For this, you will access the self.connection defined in your class.

Next, you will need a cursor to execute SQL statements. By calling self.connection.cursor(), you obtain this cursor object.

With the cursor, you can execute your SELECT statement. Write cursor.execute('SELECT * FROM books') and this will send the statement to the database. Here, the asterisk (*) represents all columns in the "books" table.

After the statement is executed, you need to retrieve the results. You do this with cursor.fetchall(), which returns all records.

Now you can return the retrieved record by writing return cursor.fetchall().
However, it is important to close the cursor and the connection after the query to free resources. This is done with cursor.close() and self.connection.close(). Both should be called at the end of your method.

To output the data, you can call the getbooks method and iterate over the return in a loop. For example, you can use for book in getbooks(): print(book) to display all books on the screen.

The records will be output as tuples. This is a common practice in Python, which can be easily converted into lists if needed. You can also access specific columns directly via the indices of the tuples or further process them.

If you have understood the principle in general, you can extend your application to add new records via INSERT statements, which will be covered in the next tutorial.
Summary – Reading Data from MySQL with Python – Step by Step
In this guide, you learned how to write effective SQL statements in Python, retrieve data using cursors, and how to properly close the connection to the database. These steps form the foundation for many further applications and adjustments.
Frequently Asked Questions
How do I retrieve data from the database?You can retrieve data by executing a SELECT statement in your method and then fetching the results with cursor.fetchall().
Why is it important to close the database connection?Closing the connection prevents resources from being unnecessarily occupied and helps avoid data loss or issues with the database.
Can I request only specific columns?Yes, instead of SELECT *, you can also specify specific columns, e.g., SELECT title, author FROM books.