Noticed something strange when working with SQLite database. In our program at the first time we launch the function for reading database data and for the first we reading the lines quantity in the table and store this in the global variable (var PetsNum).
But this variable (PetsNum) doesn't change its meaning immediately! Functions following immediately behind the database reding function can't to use the PetsNum with lines quantity value - PetsNum still zero value and only after few seconds we can see the variable change its value.
We now just use the PetsNum value little later in our code but we are just curious what the delay is related to?
... // And the following code
// Database with pets information, doganizer, food/load, breeds
function dbStartingInitialization() {
// Create or open database MyPets
db = app.OpenDatabase("MyPets");
// Create a pets table (if it doesn't exist already)
db.ExecuteSql( "CREATE TABLE IF NOT EXISTS pets (id integer primary key, name text, catordog text, breed integer, birthday text, weight integer, gender integer, common text)" );
// Read data from table
db.ExecuteSql( "SELECT * FROM pets", [], OnResult, OnError );
// Create a events table (if it doesn't exist already)
db.ExecuteSql( "CREATE TABLE IF NOT EXISTS events (id integer primary key, petName text, eventDate text, eventType integer, eventTime text, notificationType integer, comment text)" );
// Read data from table
db.ExecuteSql( "SELECT * FROM events", [], OnEventsResult, OnError );
}
function OnEventsResult( resultSet ) {
var rows = resultSet.rows;
var message = "Calendar entires:\n";
for (let iCount=0; iCount<rows.length; iCount++) {
let item = rows.item( iCount );
message += "id: " +
item.id + ", Name: " + item.petName + ", Event date: " + item.eventDate + ", Event type: " + item.eventType + "\n";
calendarArray.push({ id:
item.id, petname: item.petName, eventdate: item.eventDate, eventtype: item.eventType,
eventtime: item.eventTime, notificationtype: item.notificationType, comment: item.comment });
}
alert(message);
app.ShowPopup("Calendar events are loaded");
}
// Callback for successsful SQL execution
function OnSuccess( results ) {
app.ShowPopup("SQL are performed Ok");
}
// Callback for errors when SQL execution
function OnError( error) {
app.ShowPopup("Error: " + error.message );
}
// Callback function to handle the results of a SELECT query
function OnResult( resultset ) {
var rows = resultset.rows;
PetsNum = rows.length;
app.ShowPopup( "PetsTab= " + PetsNum + " rows");
var message = "Database entires:\n";
for (let iCount=0; iCount<PetsNum; iCount++) {
let item = rows.item(iCount);
message += "id: " +
item.id + ", Name: " +
item.name + ", catordog: " + item.catordog + ", breed: " + item.breed + "\n";
petArray.push({ id:
item.id, name:
item.name, catordog: item.catordog, birthday: item.birthday,
breed: item.breed, weight: item.weight, gender: item.gender,
common: item.common });
}
if(PetsNum==0){
petArray.push({ id: 1, name: "Kesha", catordog: "Kesha", birthday: "11",
breed: "corgi", weight: 10, gender: "girl",
common: "great" });
}
alert(message);
}
Thank you very much in advance.
Have a nice day.