admin管理员组文章数量:1394084
I am getting this error when setting up my app, creating a local database and simply inserting the first and only user (who has logged in locally). Please see ment in code for where I get the error message.
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) // Error message es here
return queryPromise;
};
return {
promise: promise,
insert_into: insert_into,
};
}]);
Where args
is simply ["user-name-string"]
, I get an error message that says:
"could not execute statement due to a constaint failure (19 UNIQUE constraint failed: user.user)
Any ideas what could have happened? Exactly the same code was running and working in a recent pure cordova project which I just ported to Ionic.
I am getting this error when setting up my app, creating a local database and simply inserting the first and only user (who has logged in locally). Please see ment in code for where I get the error message.
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) // Error message es here
return queryPromise;
};
return {
promise: promise,
insert_into: insert_into,
};
}]);
Where args
is simply ["user-name-string"]
, I get an error message that says:
"could not execute statement due to a constaint failure (19 UNIQUE constraint failed: user.user)
Any ideas what could have happened? Exactly the same code was running and working in a recent pure cordova project which I just ported to Ionic.
Share Improve this question edited Jul 16, 2015 at 6:28 novalain asked Jul 9, 2015 at 21:10 novalainnovalain 2,20921 silver badges39 bronze badges 16- can u show the table structure also which dbms are u using – Sachu Commented Jul 9, 2015 at 23:54
- r u trying to insert the same text already there in the table? can u show some example? – Sachu Commented Jul 10, 2015 at 9:31
- I'd throw a console.log statement directly inside your insert_into method and double check to make sure args is what you expect it to be and not, for example, null. – Andrew McGivery Commented Jul 10, 2015 at 18:07
- Thanks but I have done that, args is as expected and exactly as stated in the question.. – novalain Commented Jul 13, 2015 at 6:54
-
is
user
the column name? – Vamsi Prabhala Commented Jul 15, 2015 at 12:51
2 Answers
Reset to default 3It looks like you are inserting twice in your code ... here
var insert_into = function(args) {
var queryPromise = query("INSERT INTO user (user) VALUES (?)", args);
console.log("in insert_into", queryPromise) // Error message es here
return query("INSERT INTO user (user) VALUES (?)", args); <-- you did a query above, now you do it again?!?
};
The sqlite db persists when you exit the app, so each time you start the app it will try to do the insert. (I gess it worked at first launch and you got the error the second time).
As you use CREATE TABLE IF NOT EXISTS
the app does not fail on table creation but on the first insert.
You can use the pragma user_version to read/write version of your database and so know in wich case you have to create tables / insert values or if the db is already ready to go.
本文标签: javascriptSQLError 19 UNIQUE constraint failedStack Overflow
版权声明:本文标题:javascript - SQLError 19 UNIQUE constraint failed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744588308a2614329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论