【发布时间】:2017-07-13 11:46:50
【问题描述】:
我目前正在使用 sql 数据库制作一个简单的笔记保存应用程序。我有三个输入字段(标题、注释、日期)。对于标题和注释,我有输入类型 TEXT,对于日期,我有一个输入类型 DATE,单击文本区域时会出现一个弹出日历,因此用户可以从日历中进行选择,而不必键入。 但是,我在将此日期数据发送到 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!");
}
【问题讨论】:
标签: javascript sql sqlite type-conversion