简体   繁体   English

如何使用Javascript和HTML5数据库替换表中的现有联系人

[英]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 注意

  • I am not a phonegap expert 我不是电话差距专家
  • The error and success call back now have alerts 错误和成功回调现在有警报
  • executeSql now has 4 parameters (including error callback), yours seemed to have 5, but this might be because of the code alignment makes me misread the brackets executeSql现在有4个参数(包括错误回调),您的参数似乎有5个,但这可能是因为代码对齐使我误读了括号
  • confirm doesn't need if, does it? 确认是否不需要,是吗? Please check the API 请检查API
  • note that the location of contactAlreadyExist check is now still within the successCallback of executeSQL 请注意,contactAlreadyExist检查的位置现在仍在executeSQL的successCallback之内

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM