Offline database on Phonegap app

Asked

Viewed 3,364 times

5

I’m creating an app with Phonegap and need to consume/manipulate information from an existing Sqlite base within my directory WWW but in 3 days looking for tutorials only 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’s no way to do that, at least give me another way to get a database inside the phonegap and access it without needing the net.

  • 1

    A parallel question here. Through Phonegap we were able to create an application using HTML5, jQuery. But a question, maybe no-sense... after finishing the application has how to compile it in .apk to work on Android ? or when speaking app is only to use in the browser ?

  • You have to understand that phonegap creates a "browser" compiled with its source code. Technically (roughly speaking) if you pick up your site today and play within the phonegap, you will be able to "install" it on the mobile... of course it is not so simple, but in theory it is this.

1 answer

4


You can use a plugin for this.

To install use the command: Cordova plugin add https://github.com/lite4cordova/Cordova-SQLitePlugin

In order to be able to use the Database anywhere in my code, I created a variable called DB at the beginning of the index.js page, which is 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 Query any part of the Application even outside the index.js page, just use the DB variable and the query you want as in the above code.

Source: https://diariodoprogramadortda.wordpress.com/2014/03/04/trabalhando-com-sqlite-no-cordovaphonegap/

  • In case there was no ID field Insert, it becomes empty or auto increment by being Primary key?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.