UjjawalKr
UjjawalKr

Reputation: 241

Delete statement of sqlite not working in phonegap android application

i am trying to save the user credentials to a inbuilt db table and update it by deleting all the previous record whenever the login button pressed and saving the latest user details in the table. Here is my code for that :

//database //

//Transaction success callback
function createDB(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS UserInfo (username , password)'); 
}
function successCB() {
 var db = window.openDatabase("Database", "1.0", "Uinfo", 100000);
 db.transaction(queryDB, errorCB);
}

//Transaction error callback
//
function errorCB(err) {
 console.log("Error processing SQL: "+err.code);
}



//deleting previous record

function delDB(tx){
    tx.executeSql('DELETE FROM UserInfo;')
}

//Query the database

function queryDB(tx) {
 tx.executeSql('SELECT * FROM UserInfo', [], querySuccess, errorCB);
}

//Query the success callback

function querySuccess(tx, results) {
 var len = results.rows.length;
 if (len != null){
 console.log("UserInfo table: " + len + " rows found.");
 for (var i=0; i<len; i++){
     console.log("Row = " + i + " UserName = " + results.rows.item(i).username + " Password =  " + results.rows.item(i).password);
     username = results.rows.item(i).username;
     password = results.rows.item(i).password;
     document.getElementById('uNameId').value = username;
    document.getElementById('pswd').value = password;
 }
}else{

}
}


//populating table

function populateDB(tx) {
    tx.executeSql('INSERT INTO UserInfo (username, password) VALUES ('+'"'+userNameInput+'"'+', '+'"'+passwordInput+'"'+')');
}

I am clling these on deviceready:

function DeviceReady() {
    var db = window.openDatabase("Database", "1.0", "Uinfo", 100000);
    db.transaction(createDB,errorCB,successCB,delDB);

    $('#login_button').on('click', function(event) {
        userNameInput = $('#uNameId').val();
        passwordInput = $('#pswd').val();
        db.transaction(populateDB);
        window.location = "dashboard.html";     
    });
    document.addEventListener("backbutton", onBackKeyDown, false);
}

When i run the application table is successfully created, latest user details are being saved in the database but the problem is that the previous records are not being deleted. Therefore each time a new record is being saved in the database. Can't find out the reason why the delete statement isn't working properly. Any help ?

Upvotes: 0

Views: 4757

Answers (2)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Try following method to delete your previous data.

function delete_data() {

 db.transaction(delDB, errorCB);
}

//deleting previous record

function delDB(tx){
    tx.executeSql('DELETE FROM UserInfo;');
}

Use initDB() whenever page is created.

 var db ;
function initDB(){
   db = window.openDatabase("Database", "1.0", "Uinfo", 100000);
}

Now you can create table, update, delete after calling initDB() on Device Ready.

db.transaction(onSuccessMethod,onErrorMethod);

\\ change onSuccessMethod as per your need
\\ onSuccessMethod = queryDB() , populateDB() , createDB() , delDB()
\\ onErrorMethod = errorCB()

For Example:

function delete_data() {
 db.transaction(delDB, errorCB);
}

function populate_data() {
 db.transaction(populateDB, errorCB);
}

OR

U can use it directly:

function delete_data() {
     db.transaction(function (tx){
                tx.executeSql('DELETE FROM UserInfo;');}, 
                    errorCB);
    }

Upvotes: 1

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

There are two ways to perform Delete query :

  1. DELETE * from tableName
  2. DELETE FROM tableName WHERE Condition

Hope this helps.

Upvotes: 1

Related Questions