【问题标题】:Converting DATE to TEXT for use in SQL database将 DATE 转换为 TEXT 以在 SQL 数据库中使用
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 不幸的是,它仍然无法正常工作。没有出现错误,但是当单击显示注释时,仅出现标题,而不是实际注释。您想查看整个代码吗,因为错误可能在其他地方?
    • 你试过没有date 吗?如果您只插入标题和注释,它是否有效?而且一定有一些错误。尝试使用 try-catch 之类的东西,看看是否可以得到一些异常。
    • 没有日期也能完美运行。标题和注释显示没有任何问题。
    • 尝试使用alert 检查您在date 变量中获得的值。 date 也是保留关键字。因此,如果可能,请将该变量重命名为 - v_date
    • 'vdate' 正在返回日历弹出窗口中选择的正确值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多