While trying to set up my app, create a local database, and insert the very first user who has logged in locally, I encountered an error message at a specific point in the code. Here's where it happened:
angular.module("greenApp")
.service("dbService",['$q', function($q){
var db;
var promise = function(){
var deferred = $q.defer();
db = window.openDatabase('greenDB', '1.0', 'Green Database', 2*1024*1024);
db.transaction(function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS user (user TEXT PRIMARY KEY) ")
}, function(err){
alert('Something went wrong... Error: DATABASE INIT ' + err);
}, function(scc){
deferred.resolve();
})
return deferred.promise;
}
promise();
var query = function(sql, args) {
var deferred = $q.defer();
db.transaction(function(tx) {
tx.executeSql(sql, args, function(tx, results) {
deferred.resolve(results);
});
}, function(err) {
deferred.reject(err);
});
return deferred.promise;
};
var insert_into = function(args) {
var queryPromise = query("INSERT INTO user (user) VALUES (?)", args);
console.log("in insert_into", queryPromise) // This is where the error occurs
return queryPromise;
};
return {
promise: promise,
insert_into: insert_into,
};
}]);
At the point where args
is simply ["user-name-string"]
, I received the following error message:
"could not execute statement due to a constraint failure (19 UNIQUE constraint failed: user.user)
I'm puzzled by this error as the same code was functional in a recent cordova project which I migrated to Ionic. Any insights on what might be causing this issue?