Using TinyDB in App Inventor
(This post was completely rewritten and updated on October 30, 2015)
What is TinyDB?
TinyDB is a simple “database” that stores data on your phone or tablet. Unlike program variables that go away when your app is finished running or your phone is re-set, values stored in TinyDB remain on your phone for use the next time your app is run.
About Memory on your Phone or Tablet
Your smart phone or tablet typically has two primary types of memory: RAM and FLASH memory.
RAM stands for “random access memory” – but today we mostly think of RAM as memory that can be accessed very fast (as compared to Flash or hard drive memory storage). RAM retains values as long as power is applied to the RAM circuity. Once we turn off the power, the values stored in RAM are lost. (In some applications, extra batteries are used to continuously provide power to RAM even when the “normal” power is turned off.)
Flash memory retains values when the power is turned off. But access to Flash RAM is not as fast as access to conventional RAM memory.
Why is it called “Flash”? There was an early version of memory technology where the memory was erased by literally flashing it with ultraviolet light. However the inventor of Flash RAM chose the name “Flash” for different reasons. Modern Flash RAM is read, written and erased electronically.
App Inventor variables are stored in RAM memory – and the content of RAM is erased or reset whenever the power is turned off. TinyDB, on the other hand, stores values in FLASH RAM, where the values remain even when the power is turned off.
TinyDB provides a simple way to store and retrieve data efficiently and to store the data in long-term storage. TinyDB is based on the concept of a “tag” to identify the stored data, and the data value. Think of a “tag” as like using your name as your identification to look up your address:
Tag value: Martin
Value: 123 Main St, Anytown, USA
Tag value: Alexa
Value: 321 Other St, Someplace, USA
TinyDB uses the “tag” (such as Alexa) to quickly locate the corresponding value. Even if you have 100 names and addresses stored in TinyDB, TinyDB can look up the “tag” quickly and use the tag to find the corresponding value. We do not need to know how TinyDB does its look up so fast – it just does it [see Footnote 1].
In most database programs, the “tag” is known as a “key” or “key value”. App Inventor uses the name “tag” in place of “key”. As I am used to the name “key”, I tend to use “key” were I should have used “tag” in App Inventor! You will see this is the sample program, below!
To learn how to put TinyDB in operation, we construct a very simple app, described below.
We first look at how this demo program runs, from the user perspective. The demo app was run in the emulator and screen shots were taken of the emulator as the app was run. The functions of the program are simple – add an item, find an item, list all items, and erase everything.
(Please note – when I wrote this program I used the word “key” instead of “tag”. Where you see “key” – convert to App Inventor’s “tag” name!)
To add an item, enter a “tag” value and then enter a value that corresponds to that tag. For this demo, I pretended this is a simple “Task list” manager and I eventually entered 3 task items, using the task name (task1, task2, task3) as tag names, and a brief description of each “task” as the data value.
After each item is added to TinyDB, a status message is displayed:
To demonstrate finding an item in TinyDB, enter a tag value (in this example “task1”) and press the Find Item button:
The retrieved value is then displayed:
List all Items uses a ListViewer component to display all of the values in TinyDB. Note that for convenience, I wrote the code so that tag names are always converted to upper case letters: “task1” becomes “TASK1”. This way, the user may use any combination of letter cases (such as “task1”, “Task1”, “TASK1” and each of these is treated as equivalent.)
Erase All Items does exactly what the name says: it clears out all the items in the TinyDB on your phone (including other values possibly used by other apps!). This demo program does not give a warning before deleting everything – if you wish, add a Notifier dialog to ask the user to confirm the deletion.
The TinyDB control is in the Palette | Storage area. Click, drag and drop this control on to your app. An “invisible” control labeled TinyDB1 appears below your app.
The Add button event handler fetches the key value from the text box, converts to upper case, and then uses that as the “tag” for the value to store. That one block of code is all that is needed to store items into TinyDB. The rest of the code is user interface code – the on screen keyboard is hidden from view (it otherwise covers up the bottom of the screen) and a dialog box displays confirmation of the item being added to TinyDB.
Note: Again, I used txtKeyValue, with key being the traditional name for this in other databases, whereas, App Inventor calls this a “tag”. My bad.
Looking up a value is processed by the btnFind Click event handler. Again, the user entered key (tag) value is used in a call to TinyDB1.GetValue. This call returns the stored value, or, if not found, it substitutes the text that we provided for valueIfTagNotThere:
TinyDB stores a list of all the tags we have used in our TinyDB database. We can fetch these values by calling TinyDB1.GetTags, which returns a list containing all of the tags.
Our blocks code then scans through the list of tags. For each tag in the list, the code calls TinyDB1.GetValue to fetch the corresponding value. Once we now have both the tag and the value, these are combined into a tag : value pair and stored in another list. Why do we store these pairs in another list? Because to keep our code simple, we use a ListView to display the results on screen – ListView is built to display the values in a list – so we build a list of key:value pairs!
The erase function could not be easier – or more dangerous! One call to TinyDB1.ClearAll does it all!
A nice improvement to this code would be to add a dialog box on screen to confirm the deletion.
Download Source Code
Download source code: TinyDB_Simple.aia
TinyDB, TinyWebDB, Fusion Tables and Files
We can do a lot more with TinyDB – such as searching the database by the “value” (rather than just using the “tag” to find items). We can also edit (change) items in the database or delete individual items. Even though each device has only a single TinyDB database, there are programming tricks we can use to simulate having multiple, separate databases. All this is described in App Inventor 2 Databases and Files.
In addition to TinyDB, this 322 page e-book also covers the use of:
- TinyWebDB (the database is stored in the Internet “cloud” rather than on your phone, and can be shared with other users),
- Fusion Tables (a SQL-like professional class database stored in the Google Cloud, with numerous advanced database features) and
- Files (text files stored on your device.)
There is much, much more that can be done with App Inventor databases than can be shown in some blog posts!
To learn more, see “App Inventor 2 Databases and Files“. Follow the link to see the detailed description, table of contents and to download a sample chapter. The e-book is available from Amazon, Google Books and Kobo Books.
- App Inventor 2 Databases and Files (Volume 3 e-book)
Step-by-step TinyDB, TinyWebDB, Fusion Tables and Files
Buy from: Amazon, Google Books, Kobo Books
 “Data structures and Algorithms” is a sub field of computer science. This sub field has developed many optimal methods of storing and retrieving data that are vastly faster than the intuitive idea of starting at the beginning of a list and going through each item until the desired item is located. We do not need to concern ourselves here with the details of how this works; all we need to know is that TinyDB and other data base systems simplify our work by hiding the details of fast searching, storing and retrieving of data.