Reputation: 1039
I've been trying to get my cordova app working on a device with a prefilled database (using SQLite and DbCopy) and I've encountered a problem following a tutorial. When using ionic run android, to run the application on a device, the first time it works perfect, but when I run it for the second time db is null and it won't work again until I remove the Database and start all over again.
var db = null;
angular.module("alterApp", ["ionic", "ngCordova"])
.run(function ($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function(){
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar){
StatusBar.styleDefault();
}
window.plugins.sqlDB.copy("prefilledDB.db", function() {
db = $cordovaSQLite.openDB({ name: "prefilledDB.db" });
}, function(error) {
db = $cordovaSQLite.openDB({ name: "prefilledDB.db" });
});
});
})
Any thoughts ? If there is another way to use a prefilled database with Ionic then I could use it too.
Thanks in advance and sorry for my English.
Upvotes: 1
Views: 1793
Reputation: 26
I am using a plugin cordova-sqlite-ext for my project and it works fine. Here is some tips and notes from documentation:
Pre-populated database(s)
Put the database file in the www directory and open the database like (both database location and createFromLocation items are required):
var db = window.sqlitePlugin.openDatabase({name: "my.db", location: 'default', createFromLocation: 1});
IMPORTANT NOTES:
Put the pre-populated database file in the www subdirectory. (This should work well the Cordova CLI.) The pre-populated database file name must match exactly the file name given in openDatabase. This plugin does not use an automatic extension. The pre-populated database file is ignored if the database file with the same name already exists in your database file location. TIP: If you don't see the data from the pre-populated database file, completely remove your app and try it again!
Alternative: You can also use an-rahulpandey / cordova-plugin-dbcopy to install a pre-populated database
source: https://github.com/litehelpers/cordova-sqlite-ext
Upvotes: 0
Reputation: 713
I have had good success with Ionic apps and SQL lite using a "schema-version" record where I increment a number every time I modify the schema. Then, I have a database migration/sql file that corresponds to the schema version number. This pattern works pretty well for enterprise app development where the data schema changes over time.
This app provides a quality example of the pattern.
Upvotes: 0
Reputation: 652
I solved the problem with SQLite db as follows:
I put the database_name.db in the www folder and then I wrote the code in the controller. var dbName = "database_name.db"; var db = null;
var query = "SELECT * FROM table_name";
if (window.sqlitePlugin) { //device
//Copy and open the database
window.plugins.sqlDB.copy(dbName, 0, function () {
db = window.sqlitePlugin.openDatabase({name: dbName, location: 1}, function (db)` {
db.executeSql(query,[],function(result){
console.log(result.rows.length);
},function(err){
console.log("Error:" + err.message);
});
}, function (err) {
console.log('Error opening the database: ' + err.message);
});
}, function (error) {
console.error("Error copying the database: " + error.message);
db = window.sqlitePlugin.openDatabase({name: dbName, location: 1}, function (db) {
db.executeSql(query,[],function(result){
console.log(result.rows.length);
},function(err){
console.log("Error:" + err.message);
);
}, function (err) {
console.log('Error opening the database: ' + err.message);
});
});
} else{ //browser
db = window.openDatabase(dbName, 1, dbName, -1);
}
Upvotes: 0
Reputation: 856
I had a similar problem but finally I resorted to PouchDB.
You can use PouchDB [http://pouchdb.com/] and that might solve your issues.
Upvotes: 4