{"id":58,"date":"2014-08-26T22:41:58","date_gmt":"2014-08-27T05:41:58","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=58"},"modified":"2014-08-26T22:41:58","modified_gmt":"2014-08-27T05:41:58","slug":"using-tinydb-in-app-inventor","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2014\/08\/26\/using-tinydb-in-app-inventor\/","title":{"rendered":"Using TinyDB in App Inventor"},"content":{"rendered":"<p><em>(This post was completely rewritten and updated on October 30, 2015)<\/em><\/p>\n<h2>What is TinyDB?<\/h2>\n<p>TinyDB is a simple &#8220;database&#8221; 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.<\/p>\n<h2>About Memory on your Phone or Tablet<\/h2>\n<p>Your smart phone or tablet typically has two primary types of memory: RAM and FLASH memory.<\/p>\n<p>RAM stands for &#8220;random access memory&#8221; &#8211; 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 &#8220;normal&#8221; power is turned off.)<\/p>\n<p>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.<\/p>\n<p><em>Why is it called \u201cFlash\u201d?\u00a0<\/em>\u00a0There 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 &#8220;Flash&#8221; for different reasons. Modern Flash RAM is read, written and erased electronically.<\/p>\n<p>App Inventor variables are stored in RAM memory &#8211; 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.<\/p>\n<h2>Using TinyDB<\/h2>\n<p>TinyDB provides a simple way to store and retrieve data efficiently and to store the data in long-term storage.\u00a0 TinyDB is based on the concept of a &#8220;tag&#8221; to identify the stored data, and the data value. Think of a &#8220;tag&#8221; as like using your name as your identification to look up your address:<\/p>\n<p>Tag value: Martin<\/p>\n<p>Value: 123 Main St, Anytown, USA<\/p>\n<p>or<\/p>\n<p>Tag value: Alexa<\/p>\n<p>Value: 321 Other St, Someplace, USA<\/p>\n<p>TinyDB uses the &#8220;tag&#8221; (such as Alexa) to quickly locate the corresponding value. Even if you have 100 names and addresses stored in TinyDB, TinyDB can\u00a0 look up the &#8220;tag&#8221; quickly and use the tag to find the corresponding value. We do not need to know how TinyDB does its look up so fast &#8211; it just does it [see Footnote 1].<\/p>\n<p><span style=\"text-decoration: underline;\"><em>In most database programs, the &#8220;tag&#8221; is known as a &#8220;key&#8221; or &#8220;key value&#8221;. App Inventor uses the name &#8220;tag&#8221; in place of &#8220;key&#8221;. As I am used to the name &#8220;key&#8221;, I tend to use &#8220;key&#8221; were I should have used &#8220;tag&#8221; in App Inventor! You will see this is the sample program, below!<\/em><\/span><\/p>\n<p>To learn how to put TinyDB in operation, we construct a very simple app, described below.<\/p>\n<p><!--more--><\/p>\n<h2>User View<\/h2>\n<p>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 &#8211; add an item, find an item, list all items, and erase everything.<\/p>\n<p><em>(Please note &#8211; when I wrote this program I used the word &#8220;key&#8221; instead of &#8220;tag&#8221;. Where you see &#8220;key&#8221; &#8211; convert to App Inventor&#8217;s &#8220;tag&#8221; name!)<\/em><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-04-57_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1042 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-04-57_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-04-57_PM\" width=\"300\" height=\"436\" \/><\/a><\/p>\n<p>To add an item, enter a &#8220;tag&#8221; value and then enter a value that corresponds to that tag. For this demo, I pretended this is a simple &#8220;Task list&#8221; manager and I eventually entered 3 task items, using the task name (task1, task2, task3) as tag names, and a brief description of each &#8220;task&#8221; as the data value.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-06-32_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1043 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-06-32_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-06-32_PM\" width=\"300\" height=\"424\" \/><\/a><\/p>\n<p>After each item is added to TinyDB, a status message is displayed:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-07-00_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1044 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-07-00_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-07-00_PM\" width=\"300\" height=\"418\" \/><\/a><\/p>\n<p>To demonstrate finding an item in TinyDB, enter a tag value (in this example &#8220;task1&#8221;) and press the Find Item button:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-08-39_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1045 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-08-39_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-08-39_PM\" width=\"300\" height=\"417\" \/><\/a><\/p>\n<p>The retrieved value is then displayed:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-09-12_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1046 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-09-12_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-09-12_PM\" width=\"300\" height=\"416\" \/><\/a><em>List all Items<\/em> uses a <em>ListViewer<\/em> 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: &#8220;task1&#8221; becomes &#8220;TASK1&#8221;. This way, the user may use any combination of letter cases (such as &#8220;task1&#8221;, &#8220;Task1&#8221;, &#8220;TASK1&#8221; and each of these is treated as equivalent.)<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-09-38_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1047 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-09-38_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-09-38_PM\" width=\"300\" height=\"423\" \/><\/a><em>Erase All Items<\/em> 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!).\u00a0 This demo program does not give a warning before deleting everything &#8211; if you wish, add a Notifier dialog to ask the user to confirm the deletion.<\/p>\n<h2>Designer View<\/h2>\n<p>The TinyDB control is in the Palette | Storage area. Click, drag and drop this control on to your app. An &#8220;invisible&#8221; control labeled <em>TinyDB1<\/em> appears below your app.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-02-10_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1048\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-02-10_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-02-10_PM\" width=\"300\" height=\"479\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-02-29_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1049 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-02-29_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-02-29_PM\" width=\"180\" height=\"414\" \/><\/a><\/p>\n<h2>Blocks View<\/h2>\n<p>The Add button event handler fetches the key value from the text box, converts to upper case, and then uses that as the &#8220;tag&#8221; 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 &#8211; 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.<\/p>\n<p>Note: Again, I used <em>txtKeyValue<\/em>, with <em>key<\/em> being the traditional name for this in other databases, whereas, App Inventor calls this a &#8220;tag&#8221;. My bad.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-00-45_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1050 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-00-45_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-00-45_PM\" width=\"565\" height=\"323\" \/><\/a><\/p>\n<p>Looking up a value is processed by the <em>btnFind<\/em> <em>Click<\/em> event handler. Again, the user entered key (tag) value is used in a call to T<em>inyDB1.GetValue<\/em>. This call returns the stored value, or, if not found, it substitutes the text that we provided for <em>valueIfTagNotThere<\/em>:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-09_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1051 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-09_PM-1024x329.gif\" alt=\"Voila_Capture 2015-10-30_02-01-09_PM\" width=\"560\" height=\"180\" \/><\/a><\/p>\n<p>TinyDB stores a list of all the tags we have used in our TinyDB database. We can fetch these values by calling <em>TinyDB1.GetTags<\/em>, which returns a <em>list<\/em> containing all of the tags.<\/p>\n<p>Our blocks code then scans through the list of tags. For each tag in the list, the code calls <em>TinyDB1.GetValue<\/em> to fetch the corresponding value. Once we now have both the tag and the value, these are combined into a <em>tag : value<\/em> pair and stored in another list. Why do we store these pairs in another list? Because to keep our code simple, we use a <em>ListView<\/em> to display the results on screen &#8211; <em>ListView<\/em> is built to display the values in a list &#8211; so we build a list of key:value pairs!<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-31_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1052 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-31_PM-1024x502.gif\" alt=\"Voila_Capture 2015-10-30_02-01-31_PM\" width=\"558\" height=\"273\" \/><\/a><\/p>\n<p>The erase function could not be easier &#8211; or more dangerous! One call to <em>TinyDB1.ClearAll<\/em> does it all!<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-48_PM.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1053 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/08\/Voila_Capture-2015-10-30_02-01-48_PM.gif\" alt=\"Voila_Capture 2015-10-30_02-01-48_PM\" width=\"388\" height=\"124\" \/><\/a><\/p>\n<p>A nice improvement to this code would be to add a dialog box on screen to confirm the deletion.<\/p>\n<h2>Download Source Code<\/h2>\n<p>Download source code: <a href=\"http:\/\/appinventor.pevest.com\/files\/TinyDB_Simple.aia\">TinyDB_Simple.aia<\/a><\/p>\n<h2>TinyDB, TinyWebDB, Fusion Tables and Files<\/h2>\n<p>We can do a lot more with TinyDB\u00a0 &#8211; such as searching the database by the &#8220;value&#8221; (rather than just using the &#8220;tag&#8221; 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 <a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/08\/AI2Volume3Cover1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-859 alignleft\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/08\/AI2Volume3Cover1-188x300.jpg\" alt=\"AI2Volume3Cover\" width=\"188\" height=\"300\" \/><\/a>programming tricks we can use to simulate having multiple, separate databases.\u00a0 All this is described in <a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">App Inventor 2 Databases and Files<\/a>.<\/p>\n<p>In addition to TinyDB, this 322 page e-book also covers the use of:<\/p>\n<ul>\n<li>TinyWebDB (the database is stored in the Internet &#8220;cloud&#8221; rather than on your phone, and can be shared with other users),<\/li>\n<li>Fusion Tables (a SQL-like professional class database stored in the Google Cloud, with numerous advanced database features) and<\/li>\n<li>Files (text files stored on your device.)<\/li>\n<\/ul>\n<p>There is much, much more that can be done with App Inventor databases than can be shown\u00a0 in some blog posts!<\/p>\n<p>To learn more,\u00a0 see <strong><em>&#8220;<a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">App Inventor 2 Databases and Files<\/a>&#8220;. <\/em><\/strong>Follow the <a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">link<\/a> 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.<\/p>\n<ul>\n<li><strong>App Inventor 2 Databases and Files<\/strong>\u00a0<strong>(Volume 3 e-book)<\/strong><br \/>\nStep-by-step TinyDB, TinyWebDB, Fusion Tables and Files<br \/>\n<span style=\"text-decoration: underline;\"><strong>Buy from:<\/strong><\/span> <a href=\"http:\/\/amzn.to\/1UxQGSO\">Amazon<\/a>,<a href=\"https:\/\/play.google.com\/store\/books\/details\/Edward_Mitchell_App_Inventor_2_Databases_and_Files?id=ryB4CgAAQBAJ\"> Google Books<\/a>, <a href=\"https:\/\/store.kobobooks.com\/en-US\/ebook\/app-inventor-2-databases-and-files\">Kobo Books<\/a><\/li>\n<\/ul>\n<h2>Footnotes<\/h2>\n<p>[1] &#8220;Data structures and Algorithms&#8221; 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This post was completely rewritten and updated on October 30, 2015) What is TinyDB? TinyDB is a simple &#8220;database&#8221; 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 &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2014\/08\/26\/using-tinydb-in-app-inventor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using TinyDB in App Inventor<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,13],"tags":[19,22,28,68,69,87,95,130,151,176,181,199,200],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-components","category-tips","tag-android","tag-app-inventor","tag-appinventor","tag-database","tag-databases","tag-files","tag-fusion-tables","tag-mit-app-inventor","tag-programming","tag-software","tag-stem","tag-tinydb","tag-tinywebdb"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}