[英]How to replace existing contact in a table usingJavascript and HTML5 database
I am trying to add a contact in a table. 我正在尝试在表格中添加联系人。 The logic is simple , if the contact already exist confirm modal will ask for confirmation for replacement.
逻辑很简单,如果联系人已经存在,确认模式将要求确认替换。 Otherwise the contact will be inserted as a new contact.
否则,该联系人将作为新联系人插入。
The addContact()
method is called from a html page with onClick event . 使用onClick event从html页面调用
addContact()
方法。 The problem is newContact()
is called but it's not doing anything , what is the problem ? 问题是
newContact()
,但是它什么也没做,这是什么问题?
function addContact() {
var conName = document.getElementById("conName").value;
var conNumber = document.getElementById("conNumber").value;
newContact(conName, conNumber);
}
function newContact(conName, conNumber) {
var db = window.openDatabase("myDB", "1.0", "myDB", 200000);
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM contact", [], function(tx, rs) {
var contactAlreadyExist=false;
for (var i = 0; i < rs.rows.length; i++) {
var row = rs.rows.item(i);
if(row['name']==conName){
if (confirm("Want to replace contact " + conName + "?")) {
var db = window.openDatabase("qpdio", "1.0", "QPDIO", 200000);
db.transaction(function(tx) {
var sql = "Update contact set number="+conNumber+" WHERE ID="+row['id'];
tx.executeSql(sql);
contactAlreadyExist=true;
return true;
});
}
}
}
});
if(!contactAlreadyExist){
tx.executeSql("INSERT INTO contact (name, number) VALUES ('" + conName + "','" + conNumber + "')");
console.log("Here");
}
},errorAddingContact, successAddingContact);
}
function errorAddingContact(err) {
navigator.notification.alert("Error Adding Contact " + err.code, null,
"Error", "Ok");
}
function successAddingContact() {
navigator.notification.alert("Contact Saved successfully!", null,
"Information", "Ok");
}
Can you try this? 你可以试试这个吗? And let me know if there are errors?
并且让我知道是否有错误? I'll edit this answer accordingly.
我将相应地编辑此答案。 Also if you can check the code alignment (tabs/spaces especially for lines with brackets), it would be good as I think it's a bit messy, maybe due to copy-paste
另外,如果您可以检查代码对齐方式(制表符/空格,特别是带括号的行),那会很好,因为我认为这有点混乱,可能是由于复制粘贴所致
Note that 注意
Code 码
function newContact(conName, conNumber) {
var db = window.openDatabase("myDB", "1.0", "myDB", 200000);
db.transaction(
function(tx) {
tx.executeSql
(
"SELECT * FROM contact",
[],
function(tx, rs) {
var contactAlreadyExist=false;
for (var i = 0; i < rs.rows.length; i++)
{
var row = rs.rows.item(i);
if(row['name']==conName)
{
confirm
(
"Want to replace contact " + conName + "?",
function()
{
var db = window.openDatabase("qpdio", "1.0", "QPDIO", 200000);
db.transaction(function(tx) {
var sql = "Update contact set number="+conNumber+" WHERE ID="+row['id'];
tx.executeSql(sql);
contactAlreadyExist=true;
return true;
});
}
);
}
}
if (!contactAlreadyExist)
{
tx.executeSql("INSERT INTO contact (name, number) VALUES ('" + conName + "','" + conNumber + "')");
console.log("Here");
}
},
errorAddingContact
);
}
,errorAddingContact, successAddingContact);
}
function errorAddingContact(err) {
alert(err.code);
navigator.notification.alert("Error Adding Contact " + err.code, null,
"Error", "Ok");
}
function successAddingContact() {
alert(err.code);
navigator.notification.alert("Contact Saved successfully!", null,
"Information", "Ok");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.