Software Archive
Read-only legacy content
17061 Discussions

IndexedDB - Reset all database when restart the app

Helcio_R__Macedo_Pan
1,426 Views

Hey all...

I'm trying to use the IndexedDB to Sync a MySQL database with my device..

The sync, creation, update and searchs its all Ok.. BUT.. When i close my app and restart it, hes reseting the database! (Deleting all, and building it again!)

DETAIL.. It works fine on emulator, but not on the device!

 

 

 

    var request = window.indexedDB.open(databaseName, databaseVersion);
    request.onsuccess = function(event) {
      db = this.result;
        console.log("Sucesso: Banco de dados inicializado! ");
        console.log("Banco de dados: "+ db.name + " Versão: " + db.version);

        $("#aboutDBVersion").html("<b>Banco de dados: </b>"+ db.name + "<br /><b>Versão: </b>" + db.version);


        // Verifica se já foi aceitado o termo
         var count = db.transaction(['aceite_do_termo'], 'readonly').objectStore('aceite_do_termo');
             count = count.count();
         count.onsuccess = function() {
            // Se já houve aceite, pula a tela do termo
            if (count.result>0) {
                console.log("Termo já aceito");
                $("#agreement").remove();
            }
            // Senão, verifica se tem conexão
            else if(!isConnected(serverURL)) {
                 console.log("Termo NÃO aceito e usuário desconectado");
                 $("#btOkAgree").closest("div .row").html("SEM CONEXÃO COM O SERVIDOR<br />Será necessário estar conectado à internet para sincronizar o aplicativo.<br /><strong>Conecte-se à internet e reinicie o aplicativo</strong>");
                 $("#agreement").css("display","block");
            } else {
                console.log("Termo NÃO aceito");
                $("#agreement").css("display","block");
                setTimeout(verificarAtualizacao(serverURL),1);
            }

         }        
        
    };


    request.onerror = function(event) {
        console.log("Erro: Falha ao incializar o banco de dados! " + event.target.errorCode);
        console.log(event.target);
    };

    request.onupgradeneeded = function(event) {
        loadingScreen("show","Atualizando o banco de dados..<br />Aguarde!");
        clearTimeout(closeLoading); closeLoading = setTimeout(function(){ loadingScreen("hide"); }, 5000);
     
        db = event.target.result;
        var objectStore;
        //Create objectStore
        if(!db.objectStoreNames.contains("bico")) {
            objectStore = db.createObjectStore("bico", { keyPath: "id" });  
            objectStore.createIndex("nomeMaterial","nomeMaterial", {unique:false});    
            objectStore.createIndex("nome","nome", {unique:false});
        }

         var tabelaSyncControl = db.createObjectStore("sync_control", {keyPath: "id"});
    };

 

 

0 Kudos
1 Solution
Amrita_C_Intel
Employee
1,426 Views

 

Here is an example to create and retrieve data from indexedDB that you can refer to: http://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-...

you can use this example with the listview template in IntelXDK to dynamically insert data from indexedDB array to listview

View solution in original post

0 Kudos
3 Replies
Amrita_C_Intel
Employee
1,427 Views

 

Here is an example to create and retrieve data from indexedDB that you can refer to: http://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-...

you can use this example with the listview template in IntelXDK to dynamically insert data from indexedDB array to listview

0 Kudos
Helcio_R__Macedo_Pan
1,426 Views

Amrita C. (Intel) wrote:

 

Here is an example to create and retrieve data from indexedDB that you can refer to: http://blog.vanamco.com/indexeddb-fundamentals-plus-a-indexeddb-example-...

you can use this example with the listview template in IntelXDK to dynamically insert data from indexedDB array to listview

Very usefull link..

But...

I have used some codelines, changed others, but still "dropping" the database always when my app closes! :/

It works now!! Thanks A LOT!

0 Kudos
Reginaldo_P_
Beginner
1,426 Views

Hi Helcio and Amrita

I have no problems with database creation and my code used to be ok in older versions of xdk... so I have to ask: what's wrong with the newest versions? They're not supposed to work correctly with Sqlite? I really do not believe in that and just wanna know what has changed in such a fashion my database has to be created from scratch in all restart of xdk... and, of course, what can I do to fix it.

Dear Helcio, can you send me some tips about how do you solved this problem?

0 Kudos
Reply