I know that I need to import Storage and SqlStorage.
I know that I need to initialize my db: this.db = new Storage(SqlStorage);
I should be able to execute query like this.db.query('select * from table_name), etc.
What I don’t understand is the table creation!
After I did my new Storage, I tried to execute this line below in my constructor:
this.db.query('create table myTable (id integer)');
When looking in the Chrome developer tool, I can see the _ionicstorage (normal, I did not put any database name) and the “kv” table (where that table comes from?). Why ‘myTable’ is not there?
What am I missing?
Also, I noticed in the Chrome developer tool that warning:
Storage: SQLite plugin not installed, falling back to WebSQL. Make sure to install cordova-sqlite-storage in production!
Do I really need to add the SQLite plugin to my app or it is to the Chrome dev tool?
As far as i see your query method isn’t wrong, but for some reason (maybe a bug) the query from ionic 2 doesn’t work, i had to fall back to the database transaction method and to transaction.executeSql(sql, bindings):
Note: This is a workaround while the sqlStorage query function is fixed, besides the only way to make multiple transactional queries in 1 transaction, like having an entire transaction for creating each table in a database.
Note 2: Use when possible CREATE TABLE IF NOT EXISTS as the table will be created if not exist and if it does it does nothing, that way you don’t have to check before hand if the table exist.
Me either, but i supposed it would need it to be able to use the SqLite in mobile just like ionic 1 needed, so i spend half a day searching for a way to, and it was just under our noses .
@luchillo17, have you succeeded to do something with SQLStorage?
I am using the typescript version and what I noticed is this line below gives asked me for more:
this.storage = new Storage(SqlStorage);
First, it says it is missing a parameter after the SqlStorage (options)
Secondly, the first parameter should be a StorageEngine type and not a SqlStorage. I saw in the source code that SqlStorage extends StorageEngine, so it should be ok, right?
What have you done in your side? Can you show me a bit of your constructor?
@luchillo17, are you testing with the help of the browser or on the phone only? I am asking that because I can’t see anything on the browser. It is always _ionicstorage db in Web Sql (because SQlite can’t work)
Just want to add that the SQlite with key, value works. It does not create my table ‘test’ if I do this:
this.storage.query("create table if not exists test(id integer primary key unique)");
Also, if I use the key, value instead of table, can I save object and retrieve them?
I’m sorry, i actually wasn’t able to make it work with the query that storage provided, i exposed the database from it and used the transactional query to do all, even single queries, aside of working it also makes consistent my code:
queryAll(SQL) {
return new Promise((resolve, reject) => {
var promises = [];
var self_ref = this;
this.getDB().transaction((tx) => {
for (var i = 0; i < SQL.query.length; i++) {
promises.push(
self_ref.queryTx(tx, SQL.query[i], SQL.args[i])
);
}
return Promise.all(promises).then((success) => {
resolve(success);
}, (err) => {
reject(err);
});
});
});
};
Note that getDB() returns the db object that the sqlStorage generates, so basically i’m bypassing the query method to be able to make transactional queries together, i asked for help here in the forum but nobody could figure out a solution.
Hey @nicraboy, very nice tutorial! I would like to know if I will be able to test it on the Chrome desktop or I need to push my code into my phone to make it work?
[UPDATE]: I just fixed my issue and can see my table and data inside my table within Chrome. Thanks again for your help and good tutorial. Keep doing tutorials!
Please I actually created the dB with the tables but when I needed the dB in another page, it’s giving me no provider for storage error both on device and ionic serve.
Please how do I solve this. Using sqlstorage across the entire application.
Thanks!!