0
我目前正在使用sql數據庫做一個簡單的記事應用程序。我有三個輸入字段(標題,註釋,日期)。對於標題和備註我有輸入類型TEXT,並且對於日期我有一個輸入類型DATE,其中點擊文本區域時出現一個彈出式日曆,因此用戶可以從日曆中進行選擇而不必鍵入。 但是,我將這個日期數據發送到sql有一些問題,文本要麼不出現,要麼我沒有定義,如果我玩弄代碼。目前,代碼如下,注意事項不能被添加在所有:將DATE轉換爲TEXT以便在SQL數據庫中使用
//Test for browser compatibility
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 = 1MB
var mydb = openDatabase("notes_db", "0.1", "A Database of Notes", 1024 *
1024);
//create the notes 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, title TEXT, note TEXT, vdate DATE)");
});
} else {
alert("WebSQL is not supported by your browser!");
}
//function to output the list of notes in the database
function updateNoteList(transaction, results) {
//initialise the listitems variable
var listitems = "";
//get the note list holder ul
var listholder = document.getElementById("notelist");
//clear notes list ul
listholder.innerHTML = "";
var i;
//Iterate through the results
for (i = 0; i < results.rows.length; i++) {
//Get the current row
var row = results.rows.item(i);
listholder.innerHTML += "<li>" + "<b>" + "<u>" + row.title
+ "</u>" + "</b>" + "<br>" + row.note + "<br>" + "<br>" + row.vdate + "
(<a href='javascript:void(0);' onclick='deleteNote(" + row.id +
");'>Delete Note</a>)" + "</li>" + "<br>" ;
}
}
//function to get the list of notes from the database
function outputNotes() {
//check to ensure the mydb object has been created
if (mydb) {
//Get all the notes from the database with a select statement, set
outputNoteList as the callback function for the executeSql command
mydb.transaction(function(t) {
t.executeSql("SELECT * FROM notes", [], updateNoteList);
});
} else {
alert("db not found, your browser does not support web sql!");
}
function addNote() {
//check to ensure the mydb object has been created
if (mydb) {
//get the values of the title and note text inputs
var title = document.getElementById("title").value;
var note = document.getElementById("note").value;
var date = document.getElementById("date").value;
//Test to ensure that the user has entered both a title and note
if (title !== "" && note !== "") {
//Insert the user entered details into the notes table, note the use
of the ? placeholder, these will replaced by the data passed in as
an array as the second parameter
//here, the code had been as follows:
//mydb.transaction(function(t) {
//t.executeSql("INSERT INTO notes (title, note, date) VALUES (?,
//?,?)", [title, note, date]);
//alert("Note successfully added");
//});
mydb.transaction(function(t) {
t.executeSql("INSERT INTO notes (title, note, date) VALUES (?,
?,TO_DATE('', DD/MM/YYYY))", [title, note, date]);
alert("Note successfully added");
});
} else {
alert("You must enter a title and note!");
}
不幸的是,它仍然無法正常工作。沒有錯誤出現,但是當顯示註釋被點擊時,只有標題出現,但不是實際的註釋。你想看看整個代碼,或許錯誤在別處? –
你有沒有'日期'試過?如果你只插入標題和註釋,它是否工作?並且一定有一些錯誤。嘗試使用類似try-catch的東西來查看是否可以得到一些例外。 –
沒有日期,它可以很好地工作。標題和註釋顯示沒有任何問題。 –