In this chapter, we will refactor our tasklist application. It will use a database engine on the server to store items and will use jQuery's AJAX functionality to dynamically update the contents of the web application. On the server side, we will learn how to use Python's bundled SQLite database engine. On the presentation side, we will encounter jQuery UI's event system and will learn how to react to mouse clicks.
In this chapter, we shall:
Learn some benefits of using a database engine
Get familiar with SQLite, a database engine distributed with Python
Implement a password database with SQLite
Learn how to design and develop a database-driven tasklist application
Implement a test framework
Learn how to make a web application more responsive using AJAX calls
See how to implement interactive applications without<form> elements
So let's get on with it...
The advantages of a database compared to a filesystem
Storing records on a filesystem as separate files might be simple but does have several drawbacks:
You have to define your own interface for accessing these files and parsing their contents. This is much more serious than it may sound because it compels you to develop and test a lot of specific functionality that you would otherwise get more or less for free from an existing library
Accessing single files is much slower than selecting records from a table in a database. That might be workable as long as you know which record you want (as is the case in our tasklist application) but it certainly isn't workable when you want to select records based on the value of some attribute. This would necessitate opening each and every file and checking whether some attribute matches your criteria. On a data collection of hundreds of items or more, this would be prohibitively slow
Also, it is difficult to implement transactions. If we want to guarantee that a set of actions will either be successful as a whole or will be rolled back if some part of it doesn't succeed, we will have to implement very sophisticated code ourselves if we want to use files on a filesystem
When using files on a filesystem, it is a nuisance to define and maintain relations between records, and although our tasklist application is about as simple as it gets, almost any other application has more than one logical object and relations between them, so this is a serious issue.