P Irodotou P Irodotou - 2 months ago 9
AngularJS Question

Ionic project with database

I am trying to create a mobile project , which is my first project in Ionic platform.
for same , I have to learn Angular and Ionic. So I decided to make a simple mobile project.
I have googled too much for Database in mobile technology so I got many of database like- MongoDb, SQLite, Firebase etc. so I was the bit confused about which database should I use in the ionic mobile project?
And Is there any beginners documentation for any database that helps me to implement the database in my ionic project?

Thank you so much.


Instead of try to get it all at once, for training purposes, I would suggest starting only with Angular itself. Then, on a second project, try Ionic.

Some good starting points for angular are:

Then, for Ionic I used: https://thinkster.io/ionic-framework-tutorial

Now, specifically about database:
Ionic works with cordova, which maker the link between web development and mobile native functions. It makes it through plugins.

Nativelly Android and IOS support SQLite. So, if you want to use the most native resources possible, I think SQLite is the best option.

The best plugin (IMHO) is https://github.com/litehelpers/Cordova-sqlite-storage.

It's very easy to use this plugin:

On a Ionic cordova project, run

cordova plugin add cordova-sqlite-storage

Then, in your code, access the DB with

var db = window.sqlitePlugin.openDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);

Then, simply run your SQLs:

db.executeSql("select length('tenletters') as stringlength", [], function (res) {
      var stringlength = res.rows.item(0).stringlength;
      console.log('got stringlength: ' + stringlength);
      document.getElementById('deviceready').querySelector('.received').innerHTML = 'stringlength: ' + stringlength;

On the plugin site, there are more completes examples.

But, once again, I suggest learn the peaces first.


Responding to comment:

To add information on Database is simple, almost like the SELECT sample above. Just pass your data as an array, on 2ยบ argument of executeSQL(). Like this:

db.executeSql('INSERT INTO MyTable VALUES (?)', ['test-value'], function (resultSet) {
  console.log('resultSet.insertId: ' + resultSet.insertId);
  console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
}, function(error) {
  console.log('SELECT error: ' + error.message);

Take a look at the documentation, there are others examples there.