Setting up an SQLite Database on a Raspberry Pi
This project will show you how you can set up an SQLite database on a Raspberry Pi
i, OLite
‘SQLite is a self-contained relational database management system that does not rely on the typical client-server based
system,
This database system differs from MySQ\ and MariaD8 in that it is embedded directly into a program. It does not rely on a
server to function,
Instead of relying on an external system, SQLite writes its SQL data to a file that sits alongside your program
There are various reasons why SQLite is a good option for the Raspberry Pi, some of them being the following,
+ SQLite has a relatively low overhead.
+ It isa self-contained system. No extemal dependencies are required to make it function.
+ No separate server process. SQLite won't chew up your Raspberry P's RAM and CPU when not being utilize.
‘+ Zero configuration is needed making it easy to use right out of the box.
This guide will walk you through the process of installing SQLite to your Raspberry Pi as well as some simple steps on how
to utilize it
Equipment
Below is a list of the equipment we used when setting up SQLite on the Raspberry Pi
Recommended
Raspberry Pi@ Micro SD Card (86B+)
& Ethernet Cable or Wi-fi
Optional
#5 gasphernyi case
(USB Keyboard
he USB Mouse
This tutorial was tested using a Raspberry Pi4, running the latest release of Raspberry Pi OS Buster.
Installing SQLite to the Raspberry Pi
The process of installing SQLite on the Raspberry Piis straightforward and quick.
The reason for this is that SQLite is easily obtainable from the default Raspberry Pi package repository
1. To make sure we dont run into any issues winen installing SQLite, we should frst update the operating system,
You can update your Raspberry P's operating system by running the following two commands>
sudo apt update
sudo apt full-upgrade
These commands will update the list of packages on your device and then upgrade any out-of-date packages.
2. Once the update process fnishes, you can install SQLite to your Raspbery Pi
All you need to do to install SQLite is to run the following command,
sudo apt install sqlite3
You will notice that we are using SQLite 3 which is the latest major version at the time of publishing,
The exact version the Raspbian repository provides is, at the time of publishing, * 3.27.2
Using SQLite on your Raspberry Pi
Now that we have installed SQLite to the Raspberry Pi let us quickly show you how to utilize it,
Don't worry, as SQLite is incredibly simple to use and requires little to no configuration.You will need to understand the SQL language, but that is about it. We will walk you through some of the basics of SQL in
‘our next section.
1. As SQLite stores all of its data within a single file on your disk, we will need to reference that file when launching the
command line interface.
If the file does not exist, the SQLite command-line interface will generate the file for you.
Terminal $ 1B Co
sglite3 pimylifeupsglite.db
In this example, we will be creating a new SQLite database that will be stored in a file called “ piny1sfeupsolite. db
2. You will now have access to the SQLite, command line interface.
Using this tool, you will be able to issue SQL calls to your database file.
‘Additionally, you can also utilize SQLite's inbuilt functions to do stuff such as backup the database, dump the database,
and more.
To list out all of the commands, all you need to do is type in * hep All of SQlite’s inbuilt commands start with a dot (..)
to prevent a mix-up with the SQL language.
sqlite B copy
chelp
3. Once you have finished using the SQLite command-line interface on your Raspberry Fi, itis possible to quit out of the
tool
To quit out of the CU, all you need to do is type in the following command,
sqlit 1B copy
souit
Basic SQL to Use with SQLite3
Now that you have SQLite up and running on your Raspberry Pi, we can now try using some SQL
We aren't going to go super deep into this but will show a couple of commands you can use to see that your database is
working correctly.
SQLite has support for the vast majority of the SQL language. You can view what is supported by the database software on.
their website,
SQL for those who do not know stands for Structured Query Language and is designed for interacting with relational
databases like SQLite, MySQL, and MariaDB.Using this language, you can create new tables, insert new rows in the tables, update existing rows and even delete and
search through them,
Creating a Table
Before you can add any data to your brand new SQLite database, you will need to have at least one table.
Creating a table is a reasonably straightforward process and uses a syntax ike below.
sqlite I copy
CREATE TABLE yourtablename(name type, nane type);
When you create a table, it must always start with "CREATE TABLE", which will then be followed by your table's name.
The column names and data type are then stored within the brackets. Each column needs to have a name and a data type
and be separated by a comma (, ).
You can see the data types that are officially supported by the SQLite software in the datatype documentation,
Example of Creating a Table
For our example, we are going to create a table called " weather
Within this table, we will have four columns that will hold all the data we need.
1. The first column will store the id
This column will be set to * AuTOINCREMENT” and will be set as an * INTEGER".
Additionally, this will be the
RIMARY