The delay when working with SQLite

50 views
Skip to first unread message

DogPlanner GSS

unread,
Apr 28, 2026, 4:45:28 AM (4 days ago) Apr 28
to DroidScript
Dear Developers,

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?

The program is very simple:

function OnStart() {

dbStartingInitialization();

... // 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.

Best regards
Dmitry

Symbroson

unread,
Apr 28, 2026, 6:59:20 AM (3 days ago) Apr 28
to DroidScript
Callback functions typically indicate that the called function is asynchronous. This means the function initiates an operation and calls the callback a while later, when the operation is complete. In the meantime the rest of your synchronous code continues to run until the end. Only then asynchronous callbacks may be triggered.

In your example the OnStart method runs until its end, and after that the OnResult handler is called. You can confirm that with debug logs at the start and finish of your functions.

DogPlanner GSS

unread,
Apr 30, 2026, 5:01:36 PM (24 hours ago) Apr 30
to DroidScript
Dear Symbroson,

thank you very much for detailed clarification! Now it is clear. 
My previous thank you letter was not sent immidiately.
But we are very greatful!

Thank you again.
Have a nice day,

Best regards
Dmitry


вторник, 28 апреля 2026 г. в 13:59:20 UTC+3, Symbroson:
Reply all
Reply to author
Forward
0 new messages