How to Build Your Own Data Base using Blocks Part 1
AbeGetzler
7/6/2014
Motivation
Prerequisitereading
SomeSampleData
SlicingandDicing
WHERE_EQ
WHERE_EQ_NUMBER
WHERE_EQ_TEXT
DISTINCT
SELECT
Asampleappconversions
Motivation
AppInventor2doesnotprovideaccesstoanykindoflocalSQLdatabase.ItoffersTinyDB,a
key/datalocalstore,andFusionTablesblocks,forSQLaccesstoGooglesWebbasedrelational
database.TheFusionTablesinterfacerequiresyoutorememberyourcolumnnames,andto
understandandfollowSQLsyntaxtobuildqueries.
Inthispaper,wewillworkourwayuptorelationaldatabaseconceptsbybuildingonthelist
facilitiesofAppInventor.Westartwithsomefamiliaritywithsimplelists,andworkourwayupto
tables.
Prerequisite reading
http://www.appinventorblocks.com/appinventortutorialstips/appinventorlists
Some Sample Data
Herespartofaconversiontable,doneasalistoflists,alsoknownasatable.
Toreadthis,wegorowbyrow.Thefirstrowsaysthattoworkwithlengths,wecanconvertfeet
toinchesbymultiplyingby12.Likewise,thesecondrowsaysthattoworkwithlengths,wecan
convertmilestofeetbymultiplyingby5280.
Ithrewinafewtimeconversionsalsofollowingthesamepattern.Eachcolumnservesasingle
purpose:
1. TheDimension:typeofunitsbeingconverted(length,time,weight,etc.)
2. ThemeasurebeingconvertedFrom
3. ThemeasurebeingconvertedTo
4. WhatFactorwemustmultiplybytodotheconversion.
Ivedonethisasascreenshottoshowthestructureandhowyoucannestlistsinsidelists.Ive
usedtherightclickInlineInputsandExternalInputsBlockEditoroptionstogetthisrowsand
columnseffect.YoucanalsotypedataintoaspreadsheetprogramandexportthedataasaC
SV
table:
ImgoingtocheatandusetheAppInventoremulator,rightclickDoIt,andoneofthetableblocks
toextractthedatainCSVtableformat,incaseyouwanttocopymydata...
"length","feet","inches","12"
"length","miles","feet","5280"
"length","inches","cm","2.54"
"time","hours","minutes","60"
"time","minutes","seconds","60"
"time","seconds","milliseconds","1000"
"time","milliseconds","seconds","0.001"
Slicing and Dicing
DataAnalystsuseacookinganalogyfortheactofchoppingupatablebyrowandbycolumn.
Onewaytosliceupatableistopickonlythoserowsthathaveaparticularvalueinonecolumn.
InSQLthisisspecifiedwithaWHEREclause,soletsnameourproceduresusingtheword
WHERE
WHERE_EQ
Thisisaresultprocedurethatacceptsatable(alistoflists)andreturnsasmallertablecontaining
thesamecolumns,butonlytherowswhereaparticularcolumn(specifiedbycolumnnumber)
equalsaparticularvalue.
BecauseAppInventorhastwodifferentkindsofcomparisonblocks,onefornumbersandanother
fortext,wedecideearlyonwhichcomparisonwewilluseratherthandecidingonarowbyrow
basis.Peopleusuallystoreeithernumbersortextinanyonetablecolumn,andnevermixthem.
WHERE_EQ_NUMBER
WHERE_EQ_TEXT
Noticethatthesetworeturnproceduresdifferonlyinthetypeofblockusedforthecomparisonin
theinnerifthenblock.
Forconvenience,weonlyneedtoremembertousetheWHERE_EQprocedure
HeresasampleapplicationofWHERE_EQ,usingaspinnerselectionoflengthforcolumn1of
ourdatatable,togiveusjusttherowsforlengthconversions
BecauseWHERE_EQisareturnprocedureitcanbecombinedwithotherreturnprocedurecalls.
IfwewanttoreturnjusttherowsfromoursampledatawithaDimension(column1)valueof
lengthandaFromvalueoffeetwenestthecalls
Ifwewanttotestforotherconditionssuchaslessthan,greaterthan,etc.,wecanduplicate
ourWHERE_EQprocedures,andchangethenamesandtestcriteria,suchasWHERE_LT,
WHERE_GT,etc.
DISTINCT
Ifyouwanttofilteryourtabledatabyparticularcolumnvalues,youneedaprocedurethatwill
returnalistofdistinctvaluesfromoneparticulartablecolumn.Thiscanbeusedtoprimethe
ElementslistofaListPicker,ListView,oraSpinner.
Thisbuildsandreturnsaresultlistbystartingwithanemptylist,runningthroughtheinputtable,
andcheckingeachvalueintherequestedcolumnnumberagainsttheresultlistbeforeaddingit.
ThisisanexampleoftheexpressionyouwouldusetofeedtheElementsofaSpinnerorList
Pickertodecidewhichdimension(lengthortime)tousetofilterourconversiontable...
SELECT
Thisisforwhenyouwantjustsomeofthecolumnsinatable,butalltherows
from=
atable(listoflists)input
column_nos=alistofcolumnnumbersintheinputtablewewantintheresult
result=
atablewithallfromsrows,butjusttherequestedcolumnnumbers,sliddown.
A sample app conversions
https://dl.dropboxusercontent.com/u/145520/select_where_sample1.aia
AI2gallerylink
Screen shots Designer