Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:22:19+00:00 2026-06-15T14:22:19+00:00

I am developing a phonegap app which will use a sqlite database. The only

  • 0

I am developing a phonegap app which will use a sqlite database. The only updating of this database will involve its contents and not its scheme.

The approach to keep the local sqlite database updated is:

  1. There is a remote db with a table with two columns: ‘id’ and ‘sql_sentence’. It stores the set of sql_sentences to be executed locally so the local sqlite db is updated.

  2. Locally I have a var ‘local_max_id’ that keeps the id of the last sql_sentence executed locally. The first time the app is run its value is 0.

  3. Every time the app starts it compares the value of ‘local_max_id’ with the highest value in column ‘id’ of the remote DB.

  4. If the values match then local DB is up to date. Otherwise I must retrieve the set of sql_sentences with an id bigger than ‘local_max_id’, execute it and update ‘local_max_id’ to the last received row id.

The code to execute this (there are some Spanish messages but I have tried to give necessary info in English):

var min_id;
var local_max_id=0;
var sentencia_sql   = "";

function initDB(){
        //Just to make sure it requests the database info. Out when in production 
        window.localStorage.setItem("local_max_id", 0);

        // Used to see which has been the last sql_sentence id executed locally  
        local_max_id    = window.localStorage.getItem("local_max_id");

        //Call to retrieve the highest id in remote database
        $.post("http://mytests.es/phonegap-db/get-db.php", {exec:"max_id"}, treat_max_id, "json");
}


//Method that decides what to do depending on returned remote_max_id value and local_max_id.
function treat_max_id(remote_max_id){
    if(remote_max_id > local_max_id){
        $.post("http://mytests.es/phonegap-db/get-db.php", {exec:"get_sentencias", "min_id":local_max_id}, 
            function(res) {
                if(res){
                    for(j=0;j<res.length;j++){
                        sentencia_sql   = res[j].sentencia;
                        alert(sentencia_sql);
                        //The problem is here. While the alert shows the proper sql_sentence of each iteration, this variable (sentencia_sql) only has the last returned value inside transaction function, but is executed N times (being N the amount of returned sql_sentences)
                        transaction(
                            function(tx){
                                console.log(sentencia_sql);
                                tx.executeSql(sentencia_sql);   
                            },
                            errorDB,
                            succesDB2
                        );
                    }
                    window.localStorage.setItem("local_max_id", remote_max_id);
                }
                else{
                    console.error("No se ha recuperado resultado de get-db.php/get_sentencias");
                }
            },"json"
        );
    }
}

The loop that treats the result with the N sql sentences to be executed behaves in a way that is difficult for me to understand. Even though the method ‘transaction’ that is supposed to execute the sql sentence locally is inside the loop and is executed once per received row, the variable ‘sentencia_sql’ always has the same value, the last received sql sentence.

Is it normal? Should I use any instructions to control this asynchronous behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:22:21+00:00Added an answer on June 15, 2026 at 2:22 pm

    Asynchronous JavaScript takes some getting used to – code doesn’t run in the order it is written, and all iterations of a loop share the same scope.

    Adding rows to a sqlite database using a loop (Phonegap)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an iOS app. Its working properly on simulator. I'm using phonegap and
I'm developing a Phonegap app using jQuery mobile, for which I'm using a local
I am developing an app using phonegap for Android. I want this mobile application
So I am planning on developing an app using PhoneGap. I will also be
I'm developing a Android app using Phonegap - Is there any way in which
I'm currently developing an iPhone App that uses Phonegap and HTML5. I use localstorage
I am developing a jQuery Mobile and PhoneGap app. I am using this code:
I am developing a web app with phonegap and jquery. This scripts is composed
I am developing my android app which interacts with an external MSSQL database (SQL
I'm developing an app which is practically a glorified calculator. I need this app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.