object-layer / cordova-sqlite-plugin

Cordova SQLite plugin with a straightforward low-level API
Other
5 stars 15 forks source link

Create an instance of database from .db file #10

Closed andreazorzi closed 6 years ago

andreazorzi commented 6 years ago

Hi, I'm trying to open a .db file, it does not give me errors when I try to open but the query tells me that it does not find the table, the .db file is correct and contains the query table, it seems as if instead of opening it create a new database.

This is my code:

`var SQLite = window.cordova.require('cordova-sqlite-plugin.SQLite'); var sqlite = new SQLite('clienti.db');

sqlite.open(function(err) { if (err){ throw err; }

sqlite.query("SELECT Nome, Comune, ID, Logo, Tipo FROM Clienti WHERE Categoria = '"+localStorage.getItem("categoria")+"' ORDER BY Tipo, Nome", [], function(err, res) {
    if (err){
        alert("error: "+err)
        throw err;
    }

    for(var i = 0; i<res.rows.length; i++){
        // do
    }
});

});`

mvila commented 6 years ago

Hi, where is located your database file? Could you try a more minimal example which INSERT some records, and then SELECT them?

andreazorzi commented 6 years ago

Hi, I've done some tests and the database file is in the same folder as the javascript file that is running the code. I also tried with an INSERT query, but it always gives me the error that the table does not exist

mvila commented 6 years ago

The database is automatically created and it should be stored in something like Library/LocalDatabase (see https://github.com/litehelpers/Cordova-sqlite-storage#opening-a-database). Also, before using INSERT, you have to use CREATE TABLE.