Question

I'm having some trouble writing to multiple databases using phonegap 2.9.0 on android. Everything seems to work fine on iOS and in the ripple emulator but when I use my samsung galaxy S3 (and it seems to be the case with other android devices as well) I get an error, "No such table exists", when trying to write rows to the table.

I'm parsing a json object to get column names, database names etc. and then sending the data off to be written to a database. I am creating a separate database for each table since there may be a substantial amount of data written to each database.

Generally the first database / table that is written goes successfully, but then although the success callbacks are called for successive "create table" sql statements and "insert" statements, when I try to read from the db, I get the error "no such table exists "Table_CategoryClass"". All success callbacks fire, both when creating tables and writing rows. But when it comes time to read the db it seems that all tables except for the first one, are not created and that the row insert statements are not writing to any successive tables.

I was using a deferred type process to control all the async functions, but I scrapped it and put everything in success/error callbacks to make it easier for me to read and follow.

My code is as follows:

    var mobile_database = {"Category":[{ "id":"494", "CategoryClassID":"26", "Name":"Bathroom", "ParentCategoryID":"0"}, { "id":"500", "CategoryClassID":"25", "Name":"Bathroom Butler", "ParentCategoryID":"0"}, { "id":"487", "CategoryClassID":"25", "Name":"C&A", "ParentCategoryID":"0"}, { "id":"493", "CategoryClassID":"25", "Name":"Cobra", "ParentCategoryID":"0"}, { "id":"490", "CategoryClassID":"27", "Name":"Commercial", "ParentCategoryID":"0"}, { "id":"489", "CategoryClassID":"27", "Name":"Domestic", "ParentCategoryID":"0"}, { "id":"501", "CategoryClassID":"25", "Name":"Dutton", "ParentCategoryID":"0"}, { "id":"498", "CategoryClassID":"26", "Name":"Fire Equipment", "ParentCategoryID":"0"}, { "id":"507", "CategoryClassID":"25", "Name":"Genebre", "ParentCategoryID":"0"}, { "id":"496", "CategoryClassID":"26", "Name":"Geyser", "ParentCategoryID":"0"}, { "id":"509", "CategoryClassID":"25", "Name":"Gv Di Bella", "ParentCategoryID":"0"}, { "id":"491", "CategoryClassID":"27", "Name":"Industrial", "ParentCategoryID":"0"}, { "id":"504", "CategoryClassID":"25", "Name":"Isca", "ParentCategoryID":"0"}, { "id":"503", "CategoryClassID":"26", "Name":"Kitchen", "ParentCategoryID":"0"}, { "id":"495", "CategoryClassID":"25", "Name":"Kwikot", "ParentCategoryID":"0"}, { "id":"499", "CategoryClassID":"25", "Name":"Libra", "ParentCategoryID":"0"}, { "id":"502", "CategoryClassID":"25", "Name":"Plexicor", "ParentCategoryID":"0"}, { "id":"492", "CategoryClassID":"25", "Name":"Rak", "ParentCategoryID":"0"}, { "id":"497", "CategoryClassID":"25", "Name":"Safequip", "ParentCategoryID":"0"}, { "id":"506", "CategoryClassID":"25", "Name":"Salvatore", "ParentCategoryID":"0"}, { "id":"505", "CategoryClassID":"26", "Name":"Shower", "ParentCategoryID":"0"}, { "id":"488", "CategoryClassID":"26", "Name":"Toilet", "ParentCategoryID":"0"}, { "id":"508", "CategoryClassID":"25", "Name":"Vaal", "ParentCategoryID":"0"}], "CategoryClass":[{ "id":"25", "Color":"lightblue", "Name":"Brand"}, { "id":"27", "Color":"lightblue", "Name":"Category"}, { "id":"26", "Color":"lightblue", "Name":"Room/Area"}]};


function get_some_element3(){

    $("#loading").html("Setting up database");

        var mobile_data = mobile_database;//global variable containing sample data to populate db

        var column_names = get_column_names(mobile_data);//returns a json obj with array of column names "tablename": "[columns]"
        var sql_columns_obj = create_sql_columns(column_names);// returns a json obj containing {"table_name":{sql_table:"(etc...)", sql_column:"(etc...)"}, "table2":{...}}

        var create_table_array = [];

        // function sorting out column names to create tables/open databases etc... seems to be working fine.
        $.each(sql_columns_obj, function(k, v){
            var element = {};
            element["DB_Name"] = "DB_"+k;
            element["Other_DB_Name"] = "DB_"+k+"_other";
            element["Table_Name"] = "Table_"+k;
            element["sql_columns_table"] = v["sql_columns_table"];
            element["sql_columns"] = v["sql_columns"];
            element["columns"] = column_names[k];
            element["data"] = mobile_data[k];
            create_table_array.push(element);
        });

        var table_index = 0;

        //create_tables3(create_table_array[table_index]);
        create_tables3(create_table_array);

        function create_tables3( create_table_array ){
            write_to_table_new3( create_table_array );
        };
};

var super_index = 0;

//function write_to_table_new( DB_Name, Other_DB_Name, Table_Name, data, Columns, sql_columns, sql_columns_table ){
function write_to_table_new3( create_table_array ){

    var DB_Name = create_table_array[super_index]["DB_Name"];
    alert("DB_Name: "+DB_Name);
    var Other_DB_Name = create_table_array[super_index]["Other_DB_Name"];
    var Table_Name = create_table_array[super_index]["Table_Name"];
    var data = create_table_array[super_index]["data"];
    var Columns = create_table_array[super_index]["columns"];
    var sql_columns = create_table_array[super_index]["sql_columns"];
    var sql_columns_table = create_table_array[super_index]["sql_columns_table"];

    var loading = document.getElementById("loading");
    loading.innerHTML="";

    //var dfd_write = new jQuery.Deferred();// !!! deferred

    var db = null;
    var db = window.openDatabase(DB_Name, "1.0", Other_DB_Name, 8000000);// 5MB limit per ?DB?

    create_sql_table();

    function create_sql_table(){
        db.transaction(function(tx){
            alert('CREATE TABLE IF NOT EXISTS '+Table_Name+' '+sql_columns_table+'');
            tx.executeSql('DROP TABLE IF EXISTS '+Table_Name+'', null, sql_droptable_success, sql_droptable_error);
            tx.executeSql( 'CREATE TABLE IF NOT EXISTS '+Table_Name+' '+sql_columns_table+'', null, sql_table_success, sql_table_error );
        });

        function sql_table_success(){
            alert("SQL Table Success");
            write_sql_rows();
        };

        function sql_table_error(tx, err){
            alert("SQL Table Error: "+err.message);
        };

        function sql_droptable_success(){
            alert("SQL Table drop Success");
        };

        function sql_droptable_error(tx, err){
            alert("SQL Table drop Error: "+err.message);
        };

    };

    function write_sql_rows(){

        var row_index = 0;
        write_rows();

        function write_rows(){

            db.transaction(function(tx){
                var VALUES = '(';
                var data_value = "";
                for ( var j=0; j<Columns.length; j++ ){
                    var key = Columns[j];
                    if ( key != 'id' ){
                        data_value = '"'+data[row_index][key]+'"';
                    }else {
                        data_value = data[row_index][key];
                    };

                    if ( j == 0 ){
                        VALUES += data_value;
                    }else{
                        VALUES += ', '+data_value;
                    };

                };

                VALUES += ')';
                tx.executeSql('INSERT OR REPLACE INTO '+Table_Name+' '+sql_columns+' VALUES '+VALUES+'', null, row_success, row_error);
            });

            function row_success(){
                row_index += 1;
                if ( row_index < data.length ){
                    write_rows();
                }else{
                    alert("Done Writing Rows");
                    //dfd_write.resolve("success-write");
                    super_index += 1;
                    if (super_index < create_table_array.length){
                        write_to_table_new( create_table_array );
                    }else{
                        alert("Synced!");
                        super_index = 0;
                        $("#loading").hide();
                    };
                };
            };

            function row_error(tx, err){
                alert("SQL Row Error: "+err.message);
                //dfd_write.reject("fail-write");
            };

        };

    };

   // return dfd_write.promise();

};


function drop_all_tables(){
    var db = null;
    var db = window.openDatabase("DB_Products", "1.0", "DB_Products_other", 8000000);// 5MB limit per ?DB?

    db.transaction(function(tx){
        tx.executeSql('DROP TABLE IF EXISTS Table_Products');
    });

    var db = null;
    var db = window.openDatabase("DB_ProductCategory", "1.0", "DB_ProductCategory_other", 8000000);// 5MB limit per ?DB?

    db.transaction(function(tx){
        tx.executeSql('DROP TABLE IF EXISTS ProductCategory');
    });

    var db = null;
    var db = window.openDatabase("DB_Category", "1.0", "DB_Category_other", 8000000);// 5MB limit per ?DB?

    db.transaction(function(tx){
        tx.executeSql('DROP TABLE IF EXISTS Table_Category');
    });

    var db = null;
    var db = window.openDatabase("DB_CategoryClass", "1.0", "DB_CategoryClass_other", 8000000);// 5MB limit per ?DB?

    db.transaction(function(tx){
        tx.executeSql('DROP TABLE IF EXISTS Table_CategoryClass');
    });
};

As I mentioned the problem only seems to be with android. It seemed like a few other people had a similar problem, but that was on earlier versions of phonegap, not for 2.9.0, for example here.

I would greatly appreciate any help or advice, if I could offer a bounty I would.

Was it helpful?

Solution

This question is really old but I wound like to take a stab at it. Take the success callbacks out of the individual executes and add it onto the db.transaction. I had a similar issue with having multiple executes inside of a transaction where each execute had a success callback and it was messing it up. Making the change that I am suggesting solved the problem for me. I think it has to do with it being asynchronous transactions.

db.transaction(function(tx){
        alert('CREATE TABLE IF NOT EXISTS '+Table_Name+' '+sql_columns_table+'');
        tx.executeSql('DROP TABLE IF EXISTS '+Table_Name+'', null, null, errorCB);
        tx.executeSql( 'CREATE TABLE IF NOT EXISTS '+Table_Name+' '+sql_columns_table+'', null, null, errorCB );
    },errorCB,successCB);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top