开发者

Could not create table in SQLite database using Phonegap

I tried to connect to a database using phonegap. It seems that the database gets opened, but I am not able to create a table in the database. The execution seems to be stopped after the database creation. The next step using db.transaction is not working.

Code -

function createDB() {
    alert("in createDB...");
    var db = window.openDatabase("Database60", "1.0", "Phonegap Demo60", 200000);
    alert("after openDatabase...");

    db.transaction(
            function(tx)
            {
                    alert("in function(tx)...")
                    tx.executeSql('DROP TABLE IF EXISTS DEMO');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
                    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
                    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');                  
                 },
            function(tx, err)
            {
                    alert("Error processing SQL: "+err);
            }
          开发者_Python百科  )

    alert("after table creation...");
} 


try using lawnchair. most convenient of wrappers.


//this creates the database 

db.transaction(populateDB, transaction_error, populateDB_success);

function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS Registration');
var sql = "CREATE TABLE IF NOT EXISTS Registration (" + " id INTEGER PRIMARY KEY AUTOINCREMENT," + "name VARCHAR(50), " + "address VARCHAR(50),"+" age INTEGER(2), "+" birthdate DATE, "+" gender VARCHAR(10), "+" hobbies VARCHAR(100), "+" email VARCHAR(50),"+" uname VARCHAR(50),"+" pwd VARCHAR(50))";

tx.executeSql(sql);

tx.executeSql("INSERT INTO Registration  (name,address,age,birthdate,gender,hobbies,email,uname,pwd) VALUES ('"+ v1 +"','"+ v2 +"','"+ v3 +"','"+ v4 +"','"+ v5 +"','"+ v6 +"','"+ v7 +"','"+ v8 +"','"+ v9 +"')");}

function transaction_error(tx, error) {
alert("Database Error: " + error);
}

function populateDB_success() {
dbCreated = true;
alert("Successfully inserted");
db.transaction(queryDB, errorCB);
}


This works for me.Try this. May be helpful to you.

Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜