【问题标题】:unexpected behaviour while inserting into database, phonegap sqliteplugin插入数据库时​​出现意外行为,phonegap sqliteplugin
【发布时间】:2013-06-10 14:02:48
【问题描述】:

我已经编写了一个方法,首先将数据库的内容显示为列表视图,然后单击列表视图,我将显示一个弹出窗口以输入学生姓名。

输入学生姓名后,我将在数据库中检查姓名是否存在,如果姓名存在,我会提醒说姓名已经存在,否则我会将日期写入数据库

$('#storedList').children('li').off('click').on('click', function () {
                 name = $.trim($(this).text());

         $("#existing_Name").popup('open');

         $('#exisitng_Namebutton').on('click', function(e) {


            var existname = document.getElementById('existingname');
            createname = $.trim(existname.value);

         //});

            if(createname.length!=0){

            db.transaction(function(tx){
            tx.executeSql('SELECT * FROM SCHOOL WHERE  Std_name="' + name + '" and CreatedDate="'+createname+'" ', [] ,CheckingAllignSuccess,errorCB);
            }); 

            }
            else{
            alert("please enter the value");
            }
         });

        });




       }

    }


function Insertdbexisiting(tx) {
    tx.executeSql('INSERT INTO SCHOOL (Std_name, CreatedDate) VALUES ("' + name + '","' + createname + '")');
}   


function successCBexisting(){

Displaylist(); 

   //window.open("list.html#Homelist");
   $.mobile.changePage( "#Homelist", { 
            transition: "slide",
            reverse: false,
        });

} 


var CheckingAllignSuccess = function(tx, resultallign){

if(resultallign.rows.length == 0){
    db.transaction(Insertdbexisiting, errorCB, successCBexisting);
    }else
    alert("Student name already exists");
}

这在添加第一个值时可以正常工作。

在添加第二个值时,即使第二个值不存在,我也会收到一条警告消息,提示“学生姓名已存在”。

当我调试时,我注意到db.transaction(function(tx){ tx.executeSql('SELECT * FROM SCHOOL WHERE Std_name="' + name + '" and CreatedDate="'+createname+'" ', [] ,CheckingAllignSuccess,errorCB); }); 如果我第二次插入数据,则调用了 2 次。如果我第三次插入数据,它会调用 3 次

我做错了什么?如何预防?

谢谢:)

【问题讨论】:

    标签: jquery-mobile cordova


    【解决方案1】:

    我通过将off('click') 添加到$('#exisitng_Namebutton').on('click', function(e) { 进行了修复,我刚刚使它具有$('#exisitng_Namebutton').off('click').on('click', function(e) { 并且工作正常。

    这个想法是 Gajotres 在他在 SO 中的一个回答中提出的。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      相关资源
      最近更新 更多