Python Database Programming: SQLite (tutorial)

Standard

In this tutorial you will learn how to use the SQLite database management system with Python.  You will learn how to use SQLite, SQL queries, RDBMS and more of this cool stuff!

Data is everywhere and software applications use that. Data is either in memory, files or databases. One of these database management systems (DBMS) is called SQLite.  SQLite was created in the year 2000 for a missile control system (boom! did we say it was cool?) and is one of the many management systems in the database zoo. MySQL, Postregsql, Oracle, Microsoft SQL Server and Maria DB are other database animals in this zoo.  So what is this SQL?

SQL is a special-purpose programming language designed for managing data held in a databases. The language has been around since 1986 and is worth learning. The video below is an old funny video about SQL.

Why use SQLite? SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain. It is a self-contained, serverless, zero-configuration, transactional SQL database engine. The SQLite project is sponsored by Bloomberg and Mozilla.

Installation:

Verify if it is correctly installed. Copy this program and save it as test1.py

Execute with:

It should output:

What did the script above do?
The script connected to a new database called test.db with this line:

It then queries the database management system with the command

which in turn returned its version number. That line is known as an SQL query.

SQL joke. From Reddit

SQL joke. Source: Reddit

Storing data
The script below will store data into a new database called user.db

SQLite is a database management system that uses tables. These tables can have relations with other tables: it’s called relational database management system or RDBMS.  The table defines the structure of the data and can hold the data.  A database can hold many different tables. The table gets created using the command:

We add  records into the table with these commands:

The first value is the ID. The second value is the name.  Once we run the script the data gets inserted into the database table Users:

table2

Exploring the data
We can explore the database using two methods:  the command line and a graphical interface.

From console: To explore using the command line type these commands:

This will output the data in the table Users.

From GUI: If you want to use a GUI instead, there is a lot of choice. Personally I picked sqllite-man but there are many others. We install using:

We start the application sqliteman. A gui pops up.

sqliteman

Press File > Open > user.db.  It appears like not much has changed, do not worry, this is just the user interface.  On the left is a small tree view, press Tables > users. The full table including all records will be showing now.

sqliteman2

This GUI can be used to modify the records (data) in the table and to add new tables.

 The SQL language
SQL has many commands to interact with the database. You can try the commands below from the command line or from the GUI:

We can use those queries in a Python program:

This will output all data in the Users table from the database:

 

SQL joke. Source: flickr

SQL joke. Source: flickr

Creating a user information database
We can structure our data across multiple tables. This keeps our data structured, fast and organized.  If we would have a single table to store everything, we would quickly have a big chaotic mess. What we will do is create multiple tables and use them in a combination. We create two tables:

Users:

t1

Jobs:

t2

To create these tables, you can do that by hand in the GUI or use the script below:

The jobs table has an extra parameter, Uid. We use that to connect the two tables in an SQL query:

You can incorporate that SQL query in a Python script:

It should output:

If you liked this tutorial please share it using one of the buttons below :)

Leave a Reply