hello, i’m beginer to ionic framework ,i’m using cordova sqlite plugin for database my question is how to create database once only, then read and write multiple times from it, SQLite?? my question by another way how Sqlite Database should be created only once when app will be installed and executed first time then then read and write multiple times from it ? so how we can do that note that i use AngularJS ?
anyway thnk u very much
You can simply create the database by opening it. If it doesn’t exist it will be created. Then create the tables you need only if they don’t exist. Something like this:
var db = null;
var errorPopup = function(titel, desc, error)
{
$ionicPopup.alert({title: 'Error '+title, template: 'An error occurred '+desc+'. The error was: '+angular.toJson(error) });
};
if (window.cordova) {
db = $cordovaSQLite.openDB({name: 'mydatabase.db', iosDatabaseLocation: 'Documents'});
}
else {
db = window.openDatabase("mydatabase.db", "1.0", "MyDatabase", -1);
}
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS sometable (id integer primary key autoincrement, data text)").then(
function(success) {
},
function(error) {
errorPopop('open database','creating/opening the database', error);
}
);
thnk uvery much brother great work
just one point to know also
if (window.cordova) {
db = $cordovaSQLite.openDB({name: ‘mydatabase.db’, iosDatabaseLocation: ‘Documents’});
}
else {
db = window.openDatabase(“mydatabase.db”, “1.0”, “MyDatabase”, -1);
}
first work for device second for browser is this correct ? and thnks
Exactly. Only if cordova is defined (which only is the case on an actual device) it opens the the db using $cordovaSQLite. If you test the code in a browser (e.g. using ionic serve), it will use the second call.
Thank u very much brother good luck
Thanks for this answser
Hi, am very new to ionic. it is nice. I have one doubt regarding db creation. How to check the same database(with same name) is already existed .