Loading...

Blog

Latest posts

Python SQLite Tutorial — The Ultimate Guide

SQL and Python have quickly become quintessential skills for anyone taking on serious data analysis! This Python SQLite tutorial is the only guide you need to get up and running with SQLite in Python. In this post, we’ll cover off:

  • Loading the library
  • Creating and connecting to your database
  • Creating database tables
  • Adding data
  • Querying data
  • Deleting data
  • And so much more!

SQLite3 (what we’ll just call SQLite) is part of the standard Python 3 package, so you won’t need to install anything. If you’re not running Python 3, check out this link to get started.

What You’ll Create

After following this tutorial, you’ll have created a database in SQLite using Python. Specifically, this post will guide you through all the steps to create a database that covers off the following table, including all relationships:

An overview of the database we’ll create. Source: Nik Piepenbreier

Data Types Available in SQLite for Python

SQLite for Python offers fewer data types than other SQL implementations. This can be a bit restricting. However, as you’ll see, SQLite makes a lot of other things easier. Let’s take a quick look at the data types that are available:

  • NULL — Includes a NULL value
  • INTEGER — Includes an integer
  • REAL — Includes a floating-point (decimal) value
  • TEXT. — Includes text
  • BLOB. — Includes a binary large object that is stored exactly as input

From this list, you may notice a number of missing data types such as dates. Unfortunately, when using SQLite, you’re restricted to these data types.

Getting Started with SQLite in Python

Ready? Let’s get started! Source: Nik Piepenbreier

Let’s start off the tutorial by loading in the library. We can do this by using the following command:

import sqlite3

Let’s move into actually creating our database.

Creating a SQLite Database in Python

In this section of the Python SQLite tutorial, we’ll explore the different ways in which you can create a database in Python with SQLite. In order to do this, we’ll create a Connection object that will represent the database. This object is created using SQLite’s connect() function.

Let’s first create a .db file, as this is a very standard way of actually maintaining a SQLite database. We’ll represent the connection using a variable named conn. We’ll create a file called orders.db.

conn = sqlite3.connect('orders.db')

With this line of code, we’ve created a new connection object, as well as a new file called orders.db in the directory in which you’re working. If you wanted to specify a specific directory, you could write:

conn = sqlite3.connect(r'PATH-TO-YOUR-DIRECTORY/orders.db')

If the file already exists, the connect function will simply connect to that file.

Note: Notice that we included the letter “r” before the string that contains this path. This lets Python know that we’re working with a raw string, meaning that the “/” won’t be used to escape characters. You can learn more about raw strings by checking out this link.

The connect function creates a connection to the SQLite database and returns an object to represent it.

In-Memory Databases

Another way of generating databases using SQLite in Python is to create them in memory. This is a great way to generate databases that can be used for testing purposes, as they exist only in RAM.

conn = sqlite3.connect(:memory:)

However, for the purposes of this tutorial, and for most use cases you’ll run into, you’ll use the method we described earlier.

Creating a Cursor Object

Now that we’ve created a database connection object, our next task is to create a cursor object. Simply put, a cursor object allows us to execute SQL queries against a database. We’ll create a variable cur to hold our cursor object:

cur = conn.cursor()
Cursors are critical to SQLite3 in Python. Source: Nik Piepenbreier

Now that we have a cursor object, we can use it to run SQL queries in the following style:

cur.execute("YOUR-SQL-QUERY-HERE;")

Notice that we wrapped our SQL query in quotes — this is important. It doesn’t matter if we use single, double, or triple quotes. For longer queries, it’s often best to use triple quotes, as they allow us to write multi-line queries.

Creating our Tables in SQLite for Python

At this point in the Python SQLite tutorial, let’s create our first table using SQLite in Python! Now that we have a connection object (conn) and a cursor object (cur), we can create our first table. Following our database schema that we showed earlier:

A quick reminder of what our database looks like. Source: Nik Piepenbreier

We’ll start off with the users table.

cur.execute("""CREATE TABLE IF NOT EXISTS users(
userid INT PRIMARY KEY,
fname TEXT,
lname TEXT,
gender TEXT);
""")
conn.commit()

In the code above, we’re doing a number of things:

  1. Using the execute function on the cursor object to execute a SQL query
  2. Using SQL to generate a table called users
  3. The IF NOT EXISTS will help us when reconnecting to the database. The query will allow us to check if the table exists, and if it does, nothing is changed.
  4. We create four columns: useridfnamelname, and genderuserid is assigned to be the primary key.
  5. We committed the changes by using the commit function on the connection object.
Ready for some tables? Source: Nik Piepenbreier

To create our other table, we can follow a similar pattern and write the following commands:

cur.execute("""CREATE TABLE IF NOT EXISTS orders(
orderid INT PRIMARY KEY,
date TEXT,
userid TEXT,
total TEXT);
""")
conn.commit()

After executing these two scripts, your database will have two tables. We’re now ready to begin adding in data!

Adding Data with SQLite in Python

Let’s take a look at how to add data with SQLite in Python to the database we just created. Similar to the table generation query, the query to add data uses the cursor object to execute the query.

cur.execute("""INSERT INTO users(userid, fname, lname, gender) 
VALUES('00001', 'Nik', 'Piepenbreier', 'male');""")conn.commit()

Often, when we’re working within Python, we’ll have variables that hold values for us. For example, we may have a tuple that contains that information about a user which might look like this:

user = ('00002', 'Lois', 'Lane', 'Female')

If we wanted to load this data into our database, we would use a different convention:

cur.execute("INSERT INTO users VALUES(?, ?, ?, ?);", user)
conn.commit()

What we did here was replace all the values with question marks and add an additional parameter that contains the values we’re hoping to add.

It’s important to note here that the SQLite expects the values to be in tuple-format. However, the variable can contain a list, as long as the list items are tuples. For example, we could add more users using the variable:

more_users = [('00003', 'Peter', 'Parker', 'Male'), ('00004', 'Bruce', 'Wayne', 'male')]

In this case, instead of using the execute function, we’ll want to use the executemany function:

cur.executemany("INSERT INTO users VALUES(?, ?, ?, ?);", more_users)conn.commit()

If we had used the execute function on the cursor object here, the function would have assumed we were passing two items into the table directly (the two tuples), rather than two sets of four items each! Thankfully, the function would have failed in this instance, but be careful about which function you use!

SQLite and Preventing Injection Attacks

Incidentally, using the (?, ?, …) method we noted above also helps protect against SQL injection attacks. Because of this, it’s recommended to use this method over the previously noted method. It’s also easier to type out, so it’s a win-win!

Some Scripts to Load More Data

If you’re following along on your own in the Python SQLite tutorial , let’s load some more data to make the following sections more meaningful. Below are some scripts you can copy and paste to insert some sample data into both tables:

If you’re following along, load these data files in too! Source: Nik Piepenbreier

You can load this data in by using the following queries:

cur.executemany("INSERT INTO users VALUES(?, ?, ?, ?);", customers)
cur.executemany("INSERT INTO orders VALUES(?, ?, ?, ?);", orders)
conn.commit()

Selecting Data in SQLite with Python

Next in this Python SQLite tutorial , we’ll take a look at how to select data with SQLite in Python! We’ll follow a similar structure as we did to execute queries above, but we’ll add another element to it as well.

There are many ways to return data. Source: Nik Piepenbreier

Using fetchone() in SQLite with Python

Let’s begin by using the fetchone() function. We create a variable one_result to pull only result

cur.execute("SELECT * FROM users;")
one_result = cur.fetchone()
print(one_result)

This returns:

[(1, 'Nik', 'Piepenbreier', 'male')]

Using fetchmany() in SQLite with Python

Say we wanted to return more than only one result, we could use the fetchmany() function. Let’s run a different script to generate 3 results:

cur.execute("SELECT * FROM users;")
three_results = cur.fetchmany(3)
print(three_results)

This would return the following:

[(1, 'Nik', 'Piepenbreier', 'male'), (2, 'Lois', 'Lane', 'Female'), (3, 'Peter', 'Parker', 'Male')]

Using fetchall() in SQLite with Python

Similarly, we could use the fetchall() function to return all the results. If we ran the following, all results would be returned:

cur.execute("SELECT * FROM users;")
all_results = cur.fetchall()
print(all_results)

Deleting Data in SQLite with Python

Let’s learn how to delete data as well! Source: Nik Piepenbreier

Now, we’ll take a look at how to delete data using SQLite in Python. We can accomplish this using a similar structure to above. Say we wanted to delete any user with the last name ‘Parker’, we could write:

cur.execute("DELETE FROM users WHERE lname='Parker';")
conn.commit()

When we then run the query below:

cur.execute("select * from users where lname='Parker'")
print(cur.fetchall())

This prints out an empty list, confirming that the record has been deleted.

Joining Tables with SQLite in Python

Finally, let’s take a look at how to join data with a more complex query. Let’s say we wanted to generate a query that includes the first and last name for each customer on each order.

To accomplish this we let’s write the following:

cur.execute("""SELECT *, users.fname, users.lname FROM orders
LEFT JOIN users ON users.userid=orders.userid;""")
print(cur.fetchall())

Similarly, you could apply some other SQL tricks. I cover off a number of these such as sorting dataselecting data conditionally, and a more-in-depth look at joining tables.

Conclusion: Python SQLite Tutorial

In this Python SQLite tutorial, we explored everything you need to know to get started with SQLite in Python. We started off with how to load the library, explored how to create a database and tables, how to add data, how to query the tables, and how to delete data.

This post was originally from Towards Data Science.

APPLY NOW