Offline database in Phonegap app

4

I'm creating an app with Phonegap and need to consume / manipulate information from an existing SQLite database inside my WWW directory but in 3 days searching for tutorials I just found ways to create the database on time and then insert data into it . Does anyone have a "light" at the end of the tunnel there to help me? If there is not a way to do this, at least give me another way to have a database inside the phonegap and access it without needing the net.

    
asked by anonymous 07.11.2014 / 15:32

1 answer

3

You can use a plugin for this.

To install, use the command: cordova plugin add link

In order to use the Bank anywhere in my code, I created a variable named DB at the top of the index.js page, getting more or less the beginning of the page:

var db;
var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
db = window.sqlitePlugin.openDatabase({name: "DB"});
db.transaction(function(tx) {
        // Cria a Tabela "tabela_testes"
        tx.executeSql('CREATE TABLE IF NOT EXISTS tabela_teste (id integer primary key, titulo text)');
        // Adiciona um elemento a tabela
        tx.executeSql("INSERT INTO tabela_teste (titulo) VALUES (?)", ["Meu primeiro post."]);

        // Faz uma busca na tabela
        tx.executeSql("SELECT * FROM tabela_teste;", [], function(tx, res) {
            alert("Quantidade Resultados: " + res.rows.length);
            for (var i = 0;i<res.rows.length;i++){
                alert("Linha "+i+": "+res.rows.item(i).titulo);
            }
          });
    });

To make a query in any part of the Application even outside the index.js page, simply use the DB variable and the query you want as in the above code.

Source: link

    
23.02.2015 / 18:34