Title: Get started using MariaDB in Python
This post explains how to get started using the free MariaDB database in Python. It's pretty long but the steps are fairly easy.
MariaDB is a relational database management system (RDMBS). It isn't a database itself. Instead you can use it to host relational databases.
The first step is installing it.
Install MariaDB
First, you need to install MariaDB.
- Go to https://mariadb.com/products/community-server/ and click Download Community Server.
- Select the MariaDB version you want and your platform. (I'm using MS Windows 64-bit x86. The following details may be somewhat different for other platforms.)
- Click Download.
- Install. As you do, be sure to save the root password and TCP port number. That installs the MariaDB database engine and the HeidiSQL database administration tool.
Hopefully the installation runs smoothly and you can move on to using HeidiSQL to create a database.
Create a Database
HeidiSQL is a database administration program that lets you manage database such as MariaDB, PostgreSQL, SQL Server, SQLite, and others. It's automatically installed with MariaDB so you don't need to install it separately.
You can use HeidiSQL to define database users, create databases, insert data into the database, perform queries, and do just about everything else you might want to do to an RDBMS.
You can also do all of those things from a Python program, too, but that's harder and not as interactive.
When you're working on large projects, it's often useful to be able to recreate a database as needed so you can do things like test your code and then restore the database to some initial state. In that case, you may want to use a Python program or SQL scripts to do that. I'll explain how to do at least some of this in Python in later posts.
In this post, we're going to use HeidiSQL to define the database interactively. To do that, follow these steps.
- Start HeidiSQL. The MariaDB installation may have created a desktop shortcut that you can double click, or you can use the Start menu to launch it. (In non-Windows operating systems, you'll have to use their methods for finding and starting programs.)
- To connect to MariaDB, enter the root password as shown in the picture on the left below and then click Open. (If the picture's too small, right click it and select Open Image In New Tab to see it full scale.) After you open the MariaDB, you should see the result on the right below.)
The highlighted label "Unnamed" on the top of the treeview on the left is the name of your current HeidiSQL session. You can give it a name and save it if you want to reopen the same session later.
- Create a new MariaDB database. (MariaDB is a database engine, not a database. It can hold multiple databases.)
- Right-click on the Unnamed entry at the top of the treeview on the left, open the Create New menu, and pick Database.
- Enter the database name LibraryDB as shown in the following picture and click OK.
Create Tables
After you create the database, it's time to put some tables in it.
- Create the Author table.
- In the treeview on the left, right-click on the librarydb entry, open the Create New menu, and pick Table.
- Enter the table's name: Authors.
- Add the FirstName column.
- In the Columns area below, click the Add button.
- Set the following column properties.
- Name = FirstName
- Datatype = VARCHAR
- Length = 20
- Uncheck the Allow NULL property
- Repeat step C to add the LastName column: VARCHAR 20 NOT NULL
- Repeat step C to add the AuthorId column: INT 20 NOT NULL
- Set the Default value for AuthorId to AUTO_INCREMENT
Because the AuthorId field has AUTO_INCREMENT turned on, the database will automatically create new AuthorId values for any new records that you create. Just don't specify a value for AuthorId and the database will set it to the next integer value: 1, 2, 3, etc.
- Drag AuthorId to the top of the column list. (This isn't really necessary but I wanted to show that you can do it.)
- Right-click the AuthorId property, open the Create New Index menu, and select PRIMARY.
- Click Save! If you don't do this, the table won't be created.
The result should look like the following. Notice the little yellow key to the left of the AuthorId column indicating that it is the table's primary key.
- Repeat the previous steps to create the Books table giving it the following columns.
- AuthorId: INT NOT NULL
- Title: VARCHAR 100 NOT NULL NO DEFAULT
- Right-click AuthorId, open the Create New Index menu, and select KEY.
- Click Save!
The result should look like the following. Notice the little green key to the left of the AuthorId column indicating that it is a key (but not the primary key) in this table.
If you look at the bottom of HeidiSQL, you'll see a ton of SQL code. Those are the SQL commands that HeidiSQL used to create the database and tables. You probably don't want to do anything with the SQL in HeidiSQL, but you can copy and paste it into an editor where you can do things like search for specific commands. For example, if you search for CREATE TABLE Authors, you'll see something like this:
CREATE TABLE `Authors` (
`AuthorId` INT NOT NULL AUTO_INCREMENT,
`FirstName` VARCHAR(20) NOT NULL,
`LastName` VARCHAR(20) NOT NULL,
PRIMARY KEY (`AuthorId`)
)
COLLATE='utf32_general_ci'
;
As you can probably guess, this is the command that creates the Authors table. In later posts, I'll explain how you can execute commands like this in a Python program.
Add Data
Now that we have a database (LibraryDb) containing two tables (Authors and Books), it's time to add some data to the tables.
- Add Authors data.
- Select the Authors table in the treeview on the left.
- Click the Data tab on the right.
- Right-click the data area and select Insert Row insert row data.
- Leave the AuthorId blank.
- Enter FirstName = Rod
- Enter LastName=Stephens
- Click below the data to add the record. Notice that AuthorId is auto-populated as 1.
- Repeat step 3 to add Terry Pratchett. (The AuthorId is 2.)
- Repeat step 3 to add Christopher Moore. (The AuthorId is 3.)
The following picture shows a close-up of the data tab with those three records.
- Add Books data.
- Select the Books table in the treeview on the left.
- If the Data tab isn't selected, click it.
- Repeat the earlier steps to add these records.
AuthorId | Title |
1 | Beginning Database Design Solutions, Second Edition |
1 | Build Your Own Ray Tracer With Python |
1 | Beginning Software Engineering, Second Edition |
1 | Interview Puzzles Dissected |
2 | Going Postal |
2 | Making Money |
3 | Lamb: The Gospel According to Biff, Christ's Childhood Pal |
3 | Fool |
3 | The Serpent of Venice |
3 | Shakespeare for Squirrels |
The following picture shows the result.
Install the MariaDB Connector
At this point, we have a MariaDB database that includes two tables each holding some records. To use the database in Python, you need a library that can interact with the database. The library we're going to use is called MariaDB Connector/Python.
Installing the connector is easy. Simply execute the command pip install mariadb at your favorite command prompt. I use an Anaconda prompt, but you can use whatever tool you use for pip.
Python Code (Finally!)
After installing the MariaDB connector, we have finally everything set up for you to write a Python program that interacts with the database.
There are several standard steps for interacting with a database. Here are the steps for querying the data.
- Open the database to get a connection to it
- Create a cursor associated with the connection
- Use the cursor to execute a query
- Iterate through the cursor and display the results
- Close the cursor
- Close the database connection
The last two steps are particularly important. Database connections and cursors use scarce system resources and, if you don't close them properly, the system may experience run out of gas (or petrol, depending on where you live).
(When you're modifying the data (for example, adding, modifying, or deleting records), you must also be sure to commit any changes or they will be discarded. I'll talk about that in a later post.)
Here's this example's Python code.
import mariadb
# Connect to the database.
try:
conn = mariadb.connect(
user='Rod',
password='my_super_secret_password',
host='localhost',
port=3306, # Default MariaDB port
database='librarydb'
)
except mariadb.Error as e:
print(f'Error connecting to MariaDB: {e}')
else:
# Create a cursor.
cur = conn.cursor()
# Select and display data.
cur.execute('''
SELECT Title, FirstName, LastName
FROM Authors, Books
WHERE Authors.AuthorId = Books.AuthorId
''')
for title, first_name, last_name in cur:
author = f'{first_name} {last_name}:'
print(f'{author:20} {title}')
# Close the cursor and connection.
cur.close()
conn.close()
The code first tries to connect to the database. If that fails, the program displays an error message and doesn't do anything else.
If the connection succeeds, the program creates a cursor and uses its execute method to execute an SQL query. That query selects data from the Authors and Books tables where the records in the two tables have the same AuthorId values.
Next, the program uses a for loop to iterate through the cursor's results. It defines variables inside the for statement to hold the cursor's selected values.
For each cursor result, the program formats the author's name and then prints out the name and the current result's book title.
The code finishes by closing the cursor and the database connection.
The following text shows the result.
Rod Stephens: Beginning Database Design Solutions, Second Edition
Rod Stephens: Build Your Own Ray Tracer With Python
Rod Stephens: Beginning Software Engineering, Second Edition
Rod Stephens: Interview Puzzles Dissected
Terry Pratchett: Going Postal
Terry Pratchett: Making Money
Christopher Moore: Lamb: The Gospel According to Biff, Christ's Childhood Pal
Christopher Moore: Fool
Christopher Moore: The Serpent of Venice
Christopher Moore: Shakespeare for Squirrels
Conclusion
It takes a bit of work to get all this set up. The high-level steps are:
- Install MariaDB
- Create a Database
- Create Tables
- Add Data
- Install the MariaDB Connector
- Write Python code
Fortunately those steps are pretty easy. In later posts, I'll explain how you can do things like modifying the database and performing CRUD operations (Create, Read, Update, and Delete) in Python. Until then, download the example to experiment with it and to see additional details.
|