将DATE转换为TEXT以在SQL数据库中使用

时间:2017-07-13 11:46:50

标签: javascript sql sqlite type-conversion

我目前正在使用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!");
    }

1 个答案:

答案 0 :(得分:0)

看起来您正在使用HTML5 WebSQL实际上是SQLite
在SQLite中,没有日期数据类型。您必须仅将值存储为TEXT
稍后您可以使用各种Date And Time Functions进行比较,计算和显示。
您可以查看文档以获取更多详细信息。