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.

Using TinyDB

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

or

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.

User View

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!)

Voila_Capture 2015-10-30_02-04-57_PM

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.

Voila_Capture 2015-10-30_02-06-32_PM

After each item is added to TinyDB, a status message is displayed:

Voila_Capture 2015-10-30_02-07-00_PM

To demonstrate finding an item in TinyDB, enter a tag value (in this example “task1”) and press the Find Item button:

Voila_Capture 2015-10-30_02-08-39_PM

The retrieved value is then displayed:

Voila_Capture 2015-10-30_02-09-12_PMList 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.)

Voila_Capture 2015-10-30_02-09-38_PMErase 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.

Designer View

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.

Voila_Capture 2015-10-30_02-02-10_PM

 

Voila_Capture 2015-10-30_02-02-29_PM

Blocks View

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.

Voila_Capture 2015-10-30_02-00-45_PM

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:

Voila_Capture 2015-10-30_02-01-09_PM

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!

Voila_Capture 2015-10-30_02-01-31_PM

The erase function could not be easier – or more dangerous! One call to TinyDB1.ClearAll does it all!

Voila_Capture 2015-10-30_02-01-48_PM

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 AI2Volume3Coverprogramming 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

Footnotes

[1] “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.

Advertisements

33 thoughts on “Using TinyDB in App Inventor

  1. Pingback: Updated: Writing and Reading Text Files Using App Inventor | App Inventor 2: Tutorial

  2. Hi, my name is Michael. Recently I started using App Inventor 2 to make some nice ‘adult’ apps. This is a great tutorial on how to use a TinyDB, as I require one for all the data being stored. Lol. Thanks.

    Like

    • Is there a question there? Are you just starting using App Inventor or do you have some experience and you are trying to use TinyDB?

      Ed

      Like

  3. Pingback: All new tutorial: Using TinyDB in App Inventor | App Inventor 2 – Learn to Code!

    • Of course – all the code I post is intended for sharing and especially for learning – if possible, please give credit to this web site, just like citing a reference in a written paper.

      Ed

      Like

  4. is there any way to access the content of the tiny db? as an i have the rights right? i was supposed yo do is there is registration from and as an admin i want to see those who access my android app. hope you will answer this Thank you!

    Like

  5. Hello, I have a question for the list. How do we make a list with item that opens a new screen when it is selected ?
    thank you

    Like

    • With some code … when the list item is selected, you need to call the open another screen block. The open another screen item is locaed in Palette | Control blocks section in the Blocks editor.

      I have a whole chapter on using open another screen and related issues, like passing a value to and from the sub screen, in Volume 1 of my App Inventor e-book tutorial.

      Ed

      Like

  6. I would like first to commend you for the excellent work and take a big question , I’m developing a project that will have a database with aproximadamento 1000 lines . Would I like to feed that data into a text file , csv , xls , any form and use the data for each column differently. But I do not know how. Database with MySQL would the ratio for primary keys with two tables would make a while or is to take the data of each field , armazenaira in a variable and manipulate the schedule.

    In APPInverntor , how can I do this with your native database ?

    Like

    • As it sounds like you are familiar with MySQL and have ideas how to set up the MySQL database, I would recommend using Google Fusion tables. App Inventor does not, at this time, support MySQL. Instead, App Inventor supports a SQL-like database called “Fusion Tables” and they have launched a new feature in “experimental” mode, called Firebase DB.

      Fusion tables are somewhat complex to set up (you have to login in to a Google developer’s console to create your Fusion table, set up various linkages) and then write special App Inventor code to link to the Fusion table.

      This is fairly complicated and is why I have not tried to explain it online. I have a huge write up on this in Chapters 12 and 13 of my App Inventor 2: Databases and Files book. You can learn more about the e-books on this page:
      http://appinventor.pevest.com/?page_id=33

      The book also covers the use of text files for writing and reading, plus the use of CSV format files in App Inventor.

      Ed

      I hope that is useful to you and your application.

      Like

  7. Pingback: Writing to and reading from text files in App Inventor programs | App Inventor 2 – Learn to Code!

  8. Pingback: MIT App Inventor - Créer des applications mobiles & tablette And | Pearltrees

  9. Can i use tinyDB to update the status of a button in an app to the same app in other phone? if not which component can help me do that! please help

    Like

    • If I understand your question, you have an app on one phone, say Phone A, and you would like to transfer some information from the app in Phone A to an app in Phone B? In this case, to change the status of an item (a button) between the two phones.

      If I understand that is what you want to do, there are 3 ways you might do this, none of them super simple. FirebaseDB is one way, and perhaps the easiest, although FirebaseDB is still an experimental feature. I have an example using FirebaseDB here – http://appinventor.pevest.com/?p=1369

      Another way would be to keep the status information in another cloud-based database. The easiest one to use is TinyWebDB, but Fusion Tables could also be use. Both require some set up to be done in advance, and there is not a short explanation. However, my ebook goes into a lot of detail of how to use those cloud databases – see

      App Inventor 2 Databases and Files (Volume 3 e-book)
      Step-by-step TinyDB, TinyWebDB, Fusion Tables and Files

      More information on that book is available here http://appinventor.pevest.com/?page_id=33

      Ed

      Like

  10. I need your help…
    Its all about numbers logic.
    I want to make a logic. When I type ‘102’ in text box,the app has to print
    ‘1000’ in label. If type’214′ app has to print ‘210000’

    Example for relation : 102 > 10*10^2
    224 > 22*10^4
    337 > 33*10^7 etc

    Like

  11. What If you want to create a game with options of “Save Game” and “Load Game” ….How do we use the tinyDb component?

    Like

  12. Hi what if your trying to save multiple text boxes at once using the one save button how would your blocks look then we are trying to use the date inputed in the date picker as our file name also if you have anything close to this i can look at would be great please thanks

    Like

  13. Hello my name Raghunath . I have use tiny DB to store username and I am retrieving data from database but, where the username is stored .? I read some articles about tiny DB but , they have not mentioned where it will store . Although it will store in phone memory . But I want to view the username in phone memory so kindly request you to help.i want to see which folder it has stored.

    Like

  14. Hello my name Raghunath . I have use tiny DB to store username and I am retrieving data from database but, where the username is stored .? I read some articles about tiny DB but , they have not mentioned where it will store . Although it will store in phone memory . But I want to view the username in phone memory so kindly request you to help.i want to see which folder it has stored.

    Like

    • Raghunath,

      According to the MIT App Inventor documentation for TinyDB: “Each app has its own data store. There is only one data store per app. Even if you have multiple TinyDB components, they will use the same data store. To get the effect of separate stores, use different keys. You cannot use the TinyDB to pass data between two different apps on the phone, although you can use the TinyDB to share data between the different screens of a multi-screen app.”

      Basically, each app has its own TinyDB. Where is the TinyDB stored? I have not looked into that but it could be part of the app’s resources.

      Ed

      Like

      • That’s the problem – I do not know exactly where the TinyDB data is stored. Since each app has its own TinyDB database, I am thinking it is some how tied to the executable file resources. But I do not know. I’m going to be traveling for a couple of days and can not look into this until I get back. I do not know if I will be able to locate the TinyDB database on the device though. Would be interesting to know where it is.

        Ed

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s