Phonegap编写多个数据库Android“没有这样的表存在:”

时间:2014-01-20 08:50:39

标签: android cordova phonegap-build web-sql

我在使用android上的phonegap 2.9.0写入多个数据库时遇到了一些麻烦。一切似乎在iOS和波纹模拟器上工作正常但是当我使用我的三星galaxy S3(并且它似乎也是其他Android设备的情况)我得到一个错误,“没有这样的表存在”,当试图写行到表。

我正在解析一个json对象以获取列名,数据库名等,然后发送数据以写入数据库。我正在为每个表创建一个单独的数据库,因为可能会有大量数据写入每个数据库。

通常第一个写入的数据库/表成功,但是后来虽然为连续的“create table”sql语句和“insert”语句调用了成功回调,但当我尝试从db读取时,我得到了错误“没有这样的表存在”Table_CategoryClass“”。在创建表和写行时,所有成功回调都会触发。但是,当需要读取数据库时,似乎除了第一个表之外的所有表都没有创建,并且行插入语句没有写入任何连续的表。

我使用延迟类型进程来控制所有异步函数,但我废弃它并将所有内容放入成功/错误回调中,以便我更容易阅读和关注。

我的代码如下:

    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');
    });
};

正如我所提到的,这个问题似乎只与android有关。似乎其他一些人也有类似的问题,但那是早期版本的phonegap,而不是2.9.0,例如here

如果我愿意提供赏金,我将非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

这个问题真的很老了,但是我喜欢捅它。从单个执行中获取成功回调并将其添加到db.transaction。我有一个类似的问题,在一个事务中有多个执行,其中每个执行都有一个成功的回调,它搞砸了。做出我建议的改变为我解决了问题。我认为它与异步事务有关。

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);