Using TinyDB with Python

When writing your python code, you will undoubtedly find yourself in the situation where you need to store some structured data. You could just write the data to a file, but that data might take a bit of work to parse later on. Setting up a MySQL server takes a bit of work, and might be overkill for what you are trying to do. Today we will discuss how to use TinyDB in your python application to store structured data in JSON format.  And then easily retrieve that data without any fancy coding gymnastics

What is TinyDB?

TinyDB is a lightweight NoSQL engine you can use to store structured data in your Python applications. It supports storing data as JSON files in JSON on your hard disk. Or you can store your JSON data in memory for faster access times. I would not consider TinyDB a fully featured database engine. But it does just enough to be useful. You can think of it is as a free NoSQL alternative to SQL Lite.

How to install TinyDB

As with all Python projects, it is a good idea, but not required, to setup a virtual environment before proceeding. For information on how to setup virtual environments, see our article on Virtual Environments: Click Here.

Once your environment is ready, Installing TinyDB is extremely easy. Just run:

– Or  –

This will depend on if you are using Python2 or Python3.

If you don’t have pip installed, Or if you are not sure which version of Python you are using, see the article on Python Basics.

Using TinyDB

TinyDB does everything using JSON. The physical file on your hard disk is formatted in JSON, when you insert data it is with JSON, and when you query data it is in JSON. The main thing to know at this time is it uses key/value pairs separated by a colon. For example: “CarColor”:”Red”

Today we are going to use TinyDB to store information for a todo list application. The information we care about is:

  • What is the task we need to complete?
  • When do we need to complete the task by?
  • Is the task Complete?
  • How would we categorize this task?

First thing to do is import tinyDB and the required submodules:
from tinydb import TinyDB, Query

Next we need to declare a storage location for our database:

Next let’s declare a couple of records to store in our todo list application:

Notice each of the three records above have 4 key/value pairs:

  • Status
  • DueDate
  • Category
  • Description

Each key is encased in quotes. Then a Colon separates the key from the value finally the value associated with each of those items is also encased in quotes after the colon.

Inserting Records

Now that we have described the items to add to our list, we will insert them into our TinyDB database using the insert function:

If we don’t already have a variable handy to insert, you can simply add your JSON in between the curly braces:
db.insert({‘Status’:’New’,’DueDate’: ‘5/14/18’, ‘Category’: ‘Work’,’Description’:’Request a Promotion’})

You can verify your insert task worked by asking TinyDB to show everything in the database by using the db.all() function.

Searching for Records

Returning everything in the database is not that useful, especially as we start adding more items to our list. If we want to search the database for a specific record or set of records, we can use the search function:

You can search for multiple criteria at the same time, for example you can search for everything that is in the work category, and has a due date of 5/14/18:

You can search for all records which either have a category of Home, or has the a due date of 5/14/18:

You can set a variable equal to the search results:

You can then use a for loop to iterate through them:

Updating Records

Once we have completed a task, we need to mark it as done. We do that using the Update function. The update function contains two parts. First you do a search, then you update all the records that are returned as part of that search. In the example below, we will search for all tasks in the Home category, and then change the Status to Done:

Deleting Records

After we mark a task as done, we may want to clear it out of the database. To delete a record, we call the db.remove function. We then pass in a search query. Any records returned by the query will be deleted from the database.

The line below will do a search for all records where the status is set to done, then delete them:

We can delete everything from the database with the db.purge() function:

This is useful for databases that are meant to be short lived. It gives you an easy way to clear everything out and start over again. When I am doing testing, I sometimes put this up at the top of my script so I clear out any old test data before I start.

Summary

In this article we have talked about how to insert update and retrieve data from a TinyDB database. Below is a short python script that ties everything together using the examples above.

%d bloggers like this: