Hi. I have been trying to create a voting app using the tiny web database, and am trying to make it to where there are 2 options, agree and disagree, and each time that a vote is submited it adds 1 to the agree tag or if disagree is checked it adds 1 to the disagree, but I cant get it to add on to the previous value stored under each tag. How can I do this?
TinyWebDB is harder for this problem because it does not have a block to extend a list value of a tag by adding an item (like a vote).
But we can compensate for that.
Here is a tag/value structure to support voting, with questions (Best ice cream flavor?) and answer choices (vanilla, chocolate, pickle, other)…
Each tag combines a question with an allowed answer:
Best ice cream flavor/vanilla
Best ice cream flavor/chocolate
Best ice cream flavor/pickle
Best ice cream flavor/other
Cones or cups/cones
Cones or cups/cups
The choice of values is critical here. There are two approaches,
- unsuspectingly simple and sloppy, or
- scrupulous and accurate.
In both approaches, you need to ask TinyWebDB for its value for a tag before updating it, either by incrementing a numerical vote count (option 1) or adding a unique traceable vote to a list of unique vote codes under that tag (list length = vote count.)
There is a reason why option 1 is sloppy. Another voter might intervene and cast a vote for your favored tag between the time you get the tag/value and the time when your updated tag/value arrives at TinyWebDB, losing a vote for that tag/value. Professional SQL databases are built to avoid this.
With option 2, we have to remember the unique code for our vote (I like Clocks1.SystemTime at decision time) and double check after our vote was registered that it was not overwritten, keeping our votes in TinyDB (same tag as TinyWebDB, value = our unique code for that vote.
Your option choice depends on your application’s importance.
For option 1, how could I get it to incriment by 1 using the tiny web DB
To get an increment for TinyWebDB, I had to use two separate components for TinyWebDB, one to just show my counts, and another to increment the just received vote count.
Here’s my take on such an app …