lundi 11 janvier 2016

Cordova SQLite cannot execute sql statement after getting insert id

I have try these two ways to insert my record into test_table2 which are not working for me. The first way do not execute the statement inside the first execute coding. The second way has execute the statement, but it does not get the insert id. Is there anything that i have did wrong? Thank you very much.

First way:

 db.transaction(function(tx) {

        tx.executeSql('CREATE TABLE IF NOT EXISTS test_table_1 (id integer primary key AUTOINCREMENT, title text, lang text)');
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table_2 (id integer, anwser text, weight float,type varchar(255))');

        for(var key in data["testing_question"].basic.q[index].title)
        {
            tx.executeSql("INSERT INTO test_table_1 (title, lang) VALUES (?,?)", [data["testing_question"].basic.q[index].title[key],key], function(tx, res) {

                for(var key1 in data["testing_question"].basic.weight[index])
                {
                    console.log("insertId ID: "+id);
                    if(key1 != "Type")
                    {
                        tx.executeSql("INSERT INTO test_table2 (id, anwser, weight, type) VALUES (?,?,?,?)", [res.insertId, key1,data["testing_question"].basic.weight[index][key1],data["testing_question"].basic.weight[index]["Type"]], function(tx, res2) {
                        });
                    }

                 }

                console.log("insertId: " + res.insertId + " -- probably 1");    

            });

        }

    });

Second Way:

db.transaction(function(tx) {

            tx.executeSql('CREATE TABLE IF NOT EXISTS test_table_1 (id integer primary key AUTOINCREMENT, title text, lang text)');

            for(var key in data["testing_question"].basic.q[index].title)
            {
                var id = -1;
                tx.executeSql("INSERT INTO questions_basic (title, lang) VALUES (?,?)", [data["testing_question"].basic.q[index].title[key],key], function(tx, res) {
                   id = res.insertId;
                    console.log("insertId: " + res.insertId + " -- probably 1");    

                });


                 for(var key1 in data["questions"].basic.weight[index])
                 {
                            console.log("insertId ID: "+id);
                            if(key1 != "Type")
                            {
                                tx.executeSql("INSERT INTO questions_basic_weight (id, anwser, weight, type) VALUES (?,?,?,?)", [id, key1,data["testing_question"].basic.weight[index][key1],data["testing_question"].basic.weight[index]["Type"]], function(tx, res2) {
                                });
                            }
                 }                                      
            }

    });

Aucun commentaire:

Enregistrer un commentaire