0

我正在尝试从contracts db中选择所有内容,并将HTML5文本框的名字与db条目进行比较以提醒重复。我的第一个目标是将查询结果保存到 sql2 变量中。(我不能这样做,请帮助!)。

db = window.openDatabase("contactDB", "1.0", "Contact Database", 1000000); //name,version,display name, size
addButton.addEventListener(
  "click",
  function(){
    db.transaction(
    //function sql statements
    function (tx){
       ensureTableExists(tx);
       var firstName = firstNameBox.value;
       var lastName = lastNameBox.value;

       var sql = 'INSERT INTO Contacts (firstName, lastName) VALUES ("'+firstName+'","'+lastName+'")';
       tx.executeSql(sql);

           // Attempting to check for duplicates
       var sql2 = 'SELECT * FROM Contacts', WHERE firstName = "'+firstname+'";                                                                 
       tx.executeSql(sql2);
       alert(sql2);
    }, 
    //error callback
    function (err) { alert("error callback "+err.code); },
    //success callback
    function (err) { //alert("success callback "+err.code);
      loadFromDB();
    }) // db.trasaction
  } // click handler
);
4

1 回答 1

0

看看这一行:

var sql2 = 'SELECT * FROM Contacts', WHERE firstName = "'+firstname+'";

JS 变量“名字”输入错误(名称上没有大写 N)。这会将您的查询变成:

'SELECT * FROM Contacts WHERE firstName = "undefined"'

另外,为什么',之前有WHERE?我很惊讶你没有收到错误。

尝试像这样重写您的查询:

var sql2 = 'SELECT * FROM Contacts WHERE firstName = "'+firstName+'"';
于 2013-06-22T18:06:11.383 回答