Hi. I’m writing an app with AngularJS/Ionic using SQLite database. I’ve first opened my database in the app.js
APP.JS
var db = null;
angular.module('starter', ['ionic', 'starter.controllers', 'ui.router', 'ngCordova', 'leaflet-directive', 'igTruncate'])
.run(function ($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
db = $cordovaSQLite.openDB("salt.db", 1);
// Création de la table community
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS community_t (id_community integer primary key, " +
"community_name text, address text, description text, is_locked integer, website text)");
// Création de la table news
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS news_t (news_id integer primary key, " +
"news_title text, news_content text, url_news text, community_id integer, publication_date text, " +
"expiration_date text, category text)");
});
})
I have then in another file created a factory that insert a value in my table news. Here it is :
NewsService.JS
angular.module('starter')
.factory('NewsService', [ function ($cordovaSQLite) {
return {
"addNews" : function(news_content){
//var db = $cordovaSQLite.openDB("salt.db");
alert(db.toString);
// Logique d'ajout de news dans la base SQLite
var query = "INSERT INTO salt_com_news_t (news_content) VALUES (?)";
$cordovaSQLite.execute(db, query, [news_content])
.then(function (res) {
console.log("INSERT ID -> " + res.insertId);
}, function(error) {
console.error("Erreur : " + error);
});
}
}
}]);
That service is use in a controller named NewsController this way :
NewsController.JS
angular.module('starter')
.controller('NewsController', ['$scope', 'NewsService', function($scope, NewsService){
$scope.news_content = "";
$scope.saveNews = function() {
NewsService.addNews($scope.news_content);
}
}]);
But when I’m trying to execute my app in the Android emulator, I see that the database is opened but whenever I add a news Im having that error :
It feels like that database is not instantiate which I did pretty well I’m sure. One might know what can cause that please because it bothers me a lot. Thanks a lot.