2

I am working phonegap application. I had done the remember me functionality in my application. so if user used the remember me option. just i redirect the user to inbox mail page. but the database are deleted when application close and open the second time. the Issue is database value not loaded in my inbox. I don't want to delete the database when user close the app without logout. please help me.

I created the database body onload="init()"

function init() {    
    document.addEventListener("deviceready", onDeviceReady, false);   
    delete init;
}


function onDeviceReady() {    
    if(window.localStorage.getItem("remember") == null)
    {        
    db = window.openDatabase("SampleDB","0.1","Name DB", 5000000);
    db.transaction(createDB, errorDB, txSuccess);
    }

}
user2499653
  • 91
  • 1
  • 7
  • can you show us the createDB() function call. I am not sure, but my feeling is that window.localstorage functionality will be valid only for that session. So when you close that application the data in localStorage will be lost. so everytime you open the the app the db.transaction(CreateDB.... will be executed and if the CreateDB contains a create table statement it will be executed. Therefore I would like to see what the createDB function does. – frank Jun 30 '14 at 07:41
  • function createDB(tx) { tx.executeSql("DROP TABLE IF EXISTS zergId"); tx.executeSql("CREATE TABLE mytable(id INTEGER, user_id , s_id , s_name, r_id , r_name, e_id , subject, char_id , char_name, g_id , g_name, parent_id INTEGER , is_viewed ,is_parent, is_garbage, is_deleted, message, created_date)"); } This is my createDB(); – user2499653 Jun 30 '14 at 10:13
  • Just I used only the phonegap document for reference then i created like this. somebody asked create the sqlite plugin. but I won't install the plugin. can you please tell me which is the correct way to handle the sqlite database and Its correct or not? what i did my functionality. – user2499653 Jun 30 '14 at 10:22
  • Is it possible to find where we can see our data in db for android – sarabu Jul 17 '14 at 11:30

1 Answers1

2

In your createDB() call you have a DROP table sql statement. So every time you open your application the DROP table and CREATE TABLE will get executed. Also I have noticed that DROP statement as a different table "zergid" than the CREATE statement "mytable". Is this your requirement?

function createDB(tx) { 
    tx.executeSql("DROP TABLE IF EXISTS zergId"); 
    tx.executeSql("CREATE TABLE mytable(id INTEGER, user_id , s_id , s_name, r_id , r_name, e_id , subject, char_id , char_name, g_id , g_name, parent_id INTEGER , is_viewed ,is_parent, is_garbage, is_deleted, message, created_date)"); 
}

If you want to create table only once you can use the IF NOT EXISTS clause.

tx.executeSql("CREATE TABLE IF NOT EXISTS mytable(id INTEGER ...

Hope this helps.

frank
  • 3,180
  • 3
  • 16
  • 18
  • ok thanks your for answer actually when i start the app i used this query like this but some error occurred that's why i changed. Table name its not a problem. I just changed when i post this command. i will try this. – user2499653 Jun 30 '14 at 14:57
  • Hi, My problem not solved. Database value did not loaded again. I made a all changes. I deleted the drop query and changed create table query like your comments. when i going to redirect the second page database not loaded correctly. – user2499653 Jul 01 '14 at 06:34
  • $(document).on('pagebeforeshow', '#messageList', function() { checkPreAuth(); }); function checkPreAuth() { /* console.log("checkPreAuth");*/ if(window.localStorage.getItem("remember") == "true") { if(window.localStorage.getItem("username") != undefined && window.localStorage.getItem("password") != undefined) { $.mobile.changePage("Inbox.html"); } $('#messageList').live('pageshow', function(event) { db.transaction(queryDB,errorCB); }); – user2499653 Jul 01 '14 at 06:34
  • what is the error that you are getting? You need to provide us the error/message that you are getting. It is impossible for us to guess where you are making a mistake. – frank Jul 01 '14 at 07:59
  • I am not getting error. the database value not load when i select the from the sqlite database. function queryDB(tx){ var userid = window.localStorage.getItem("userId"); tx.executeSql('SELECT * FROM mytable where user_id="'+ userid +'" ORDER BY message_id DESC',[],querySuccess,errorCB); } function querySuccess(tx,results){ $.each(results.rows,function(index) { var row = results.rows.item(index); alert(row['message_id']); }); } – user2499653 Jul 01 '14 at 09:21
  • how can i see my database table. i don't know values are saved or not. I have only select the table values. but i don't know select query problem or table values null. can you please tell me how can i see my database table – user2499653 Jul 01 '14 at 09:42
  • Have a look over [here](http://stackoverflow.com/questions/19570868/storage-of-sqlite-database-using-android-and-phonegap) and [here](http://stackoverflow.com/questions/12708705/location-of-sqlite-database-in-eclipse-for-android-using-phonegap) for seeing your database – frank Jul 01 '14 at 10:22
  • I have problem now when comment this line. tx.executeSql("DROP TABLE IF EXISTS mytable"); the table values are not loaded correctly when different user login time . If i uncomment this line values loaded. I have so match confusion about this Sqlite database. Please help me. – user2499653 Jul 01 '14 at 10:32
  • you need to show your entire code. It is difficult for us to know where you are going wrong. I can see in your previous comments that you are using a where clause `where user_id=" + userid`, so you will only get data for that user. Try to use console.log() call for debugging your application – frank Jul 01 '14 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56621/discussion-between-user2499653-and-frank). – user2499653 Jul 02 '14 at 05:32
  • Hi i can share my code. please help me how to use Sqlite database. i am really confuse. I just referred the phonegap documentation so that's way i tried. My problem is the database values are not loaded correctly. 1. If i delete the "Drop exist table query" I can't get the value from the exist table. – user2499653 Jul 02 '14 at 06:14
  • If i close the application fully,i am dropping the entire table and again if i login in i am getting the fresh data from server and working fine.But if i log out instead of closing the entire app,i am not droping the table by the time if i again login,i can see the existing value in sqlite database and i am not updated with the fresh data from server.I want to get fresh data even if i logout instead of closing te entire app.Please help me to so this. – user2499653 Jul 02 '14 at 06:14