2017-08-03 71 views
0

我有一個標題和一個筆記的輸入字段,並根據當前日期自動創建日期。但是,我希望數據庫進行更新,以便如果數據庫中已有今天的日期,那麼同一日期的任何新項目都會替換它。這是可能的,還是必須在同一日期進行多次輸入?用一個新項目替換webSQL數據庫中的一個項目,如果它具有相同的日期

document.getElementById("hide").style.visibility = "hidden"; 
document.getElementById("enterBtn").addEventListener("click", addEntry); 
document.getElementById("display").addEventListener("click", display); 
document.getElementById("hide").addEventListener("click", hidden); 
//document.getElementById("listDiv2").innerHTML = " "; 

//var date = new Date(); 
//date = new Date(date).toUTCString(); 
//date = date.split(' ').slice(0, 5).join(' '); 


if (window.openDatabase) { 
    //Create the database the parameters are 1. the database name 2.version 
    number 3. a description 4. the size of the database(in bytes) 1024 x 1024 = 1 MB 
    var mydb = openDatabase("wellness", "0.1", "Wellness App", 1024 * 1024); 

    //create the entry table using SQL for the database using a transaction 
    mydb.transaction(function(t) { 
     t.executeSql("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY 
      KEY ASC, v_date TEXT, title TEXT, note TEXT) 
     "); 
    }); 

} else { 
    myApp.alert("init if statement error"); 
} 


//function to output the list of entry in the database 

function updateEntryList(transaction, results) { 
    //initialise the listitems variable 

    var listitems = " "; 

    var listholder = document.getElementById("listDiv2"); 

    //clear entry list ul 
    listholder.innerHTML = " "; 

    var i; 

    //Iterate through the results 
    for (i = 0; i < results.rows.length; i++) { 

     var row = results.rows.item(i); 

     listholder.innerHTML += "<li>" + "<p>" + '<h2>' + '<u>' + row.title + 
      '</u>' + '</h2>' + "<p>" + '<h3>' + 'Note: ' + row.note + '</h3>' + "<p>" + 
      row.v_date + "<p>" + "__________________" + "</li>"; 
    } 
} 

function clearText() { 
    //document.getElementById('v_date').value = ''; 
    document.getElementById('title').value = ''; 
    document.getElementById('note').value = ''; 
} 

//function to get the list of entry from the database 

function outputEntry() { 
    //check to ensure the mydb object has been created 
    if (mydb) { 
     //Get all the entry from the database with a select statement, set 
     outputEntryList as the callback 
     function 
     for the executeSql command 
     mydb.transaction(function(t) { 
      t.executeSql("SELECT * FROM notes", [], updateEntryList); 
      //myApp.alert("outputEntry called"); 
     }); 
    } else { 
     myApp.alert("outputEntry error"); 
    } 
} 


function display() { 
    outputEntry(); 
    document.getElementById("display").style.visibility = "hidden"; 
    document.getElementById("listDiv2").style.visibility = "visible"; 
    document.getElementById("hide").style.visibility = "visible"; 
} 

function hidden() { 
    document.getElementById("display").style.visibility = "visible"; 
    document.getElementById("listDiv2").style.visibility = "hidden"; 
    document.getElementById("hide").style.visibility = "hidden"; 
} 
//function to add the car to the database 

function addEntry() { 
    //check to ensure the mydb object has been created 
    if (mydb) { 
     //myApp.alert("if statement good"); 
     //var d = new Date(); 
     //var date = d.toDateString(); 
     //get the values of the make and model text inputs 
     //var v_date = document.getElementById("v_date").value; 
     var title = document.getElementById("title").value; 
     var note = document.getElementById("note").value; 

     v_date = new Date; 
     v_date = new Date(v_date).toUTCString(); 
     v_date = v_date.split(' ').slice(0, 4).join(' ') 

     //date = new Date(date).toUTCString(); 
     //date = date.split(' ').slice(0, 5).join(' '); 

     //myApp.alert("all elements got by id"); 

     //Test to ensure that the user has entered both a make and model 
     if (title !== "" && note !== "") { 

      //Insert the user entered details into the entry table, note the use 
      of the ? placeholder, these will replaced by the data passed in as an 
      array as the second parameter 
      mydb.transaction(function(t) { 
       t.executeSql("INSERT INTO notes (v_date, title, note) VALUES (?, ?, ?)", [v_date, title, note]); 

       myApp.alert("Your Entry Added"); 
      }); 
     } else { 
      myApp.alert("You must enter values!"); 
     } 

    } else { 
     myApp.alert("add entry error"); 
    } 
    clearText(); 
} 

回答

0

爲什麼不使用字符串作爲主鍵?

var date = new Date(); 
var key = date.getFullYear() + date.getMonth() + date.getDate() 

,並使用該密鑰與插入或更換INTO

相關問題