A Flat Database (part 4) ->advanced

Please read part 1, part 2, and part3, before reading this tutorial. The 4th and final part of this series will explain how to actually save and retrieve a record.  Everything to do with both can be done with the Code block and JavaScript.

Saving a record in our Flat Database

When should you save a record? Obviously, you should always save a record after you have all of the data that makes up that record. There could be instances where you would want to save each bit of information as soon as you receive it. This group of tutorials though, has been based on storing test results. So, in that case, you should save the record as soon as possible after completion of the quiz.

How do you save a record? With the example I created, it is done in a Code block. Here’s the code I used, with this demo skill. Rather than explain it, I included JavaScript Comments (lines that begin with 2 slashes //) to do the explaining for me.

// increase record number value
// if no questions were answered correctly put in a zero instead of nothing
if (correctQs==””) correctQs=”0″;
// add each value to the next record (recNums has been increased)
// the square brackets signify we are adding them to an array
//correctQs is a list of all questions answered correctly
flat_db[recNums]= [userName,today,score,numOfQs,numCorrect,correctQs];
// this stores the highest record number in element [0][0]
// next, we are storing ONE user name, in element zero of an array
// but with a little bit of JavaScript, you could use the previous line of code to store a list of users in flat_db[0][1] and then at some future time you would be able to select only the records for any one user

Retrieving a record in our Flat Database

How do you retrieve a record?

The next bit of JavaScript code will allow you to choose a record, and retrieve it.Her, I made it a random record. Probably not very useful getting a random record, but I put that in to show you a function for retrieving a random number.

// the next four lines is a function for finding a random number between max and min (maximum and minimum)
function ourRandomRange(max,min)
return Math.floor(Math.random() * (max – min + 1)) + min;
// set max to the present (highest) record number
var max=recNums;
// set min to 1 (because I decided to use the zeroth record to store the highest record number)
var min=1;
// set randInt to a random number, using the function above
var randInt=ourRandomRange(max,min);
// set the skill’s userName value to the one stored at the element stored at randInt (the random number)
// did the same with the rest of the variables USING THE SAME RECORD NUMBER

What does our Flat Database look like?

First, trust me when I say everything works.

The bad news is that an Alexa skill can NOT understand what an array is! But that’s all right. The Code block WILL! Therefore when you take an element of an array like flat_db[randInt][3] in the code above and assign it to a known variable like {numOfQs} everything will work out OK.

You should also have noticed that in Code blocks variables (rightly) should NOT  be surrounded by curly brackets ‘{}’ while in most other blocks, the curly brackets are essential.

What you will see if you look at a printout of a database will look really weird. Here’s what a sample 3 record  looks like.

3,bill,bill,2/1/2019 21:07GMT,0,3,0,0,bill,2/1/2019 21:07GMT,5,3,1,202,bill,2/1/2019 21:07GMT,15,4,3,211,212,213

It appears to be just a bunch of numbers, but if you look at it knowing the order of the elements in each record, you should be able to see the pattern.


It would be possible to take this much farther, but this is the final part of the series.  Hopefully this will have inspired you to make a flat database and make use of an array with a skill of your own.

Here are a couple of ways that you could expand on what is provided in this series of lessons.

  1. You could make flat_db[0][1] into an array of all the people using your skill on one Echo. That would be great in a classroom situation.
  2. You could use flat_db[0][2] as an array of all questions a person answers correctly, then refer to that list each time a question is asked to make sure questions previously answered correctly are never asked again.



About This Site

This site was created as a place for users of our Alexa skill to find more information about creating their own skills, without programming.