尝试使用WebSql将数据存储在本地存储中

时间:2012-10-10 08:15:17

标签: android jquery html5

我在html表单中插入了值。即使在成功插入数据之后,它在chrome的开发人员工具中也不可见。我应该检查一些设置还是我的浏览器有任何缓存问题。我的数据库和表已创建,但我的值未插入。我在下面提供我的代码。

$(document).ready(function() {

     var db = openDatabase("todolist", 1.0, "To Do List Database", 100 * 1024);
     db.transaction(function(transaction) {
         transaction.executeSql("CREATE TABLE IF NOT EXISTS list(" + "ID INTEGER PRIMARY KEY ASC," + " name TEXT NOT NULL," + " description TEXT," + " due DATETIME," + " start DATETIME);", []);
     });

     $("#submit").click(function() {
         var name = $("#taskname").val();
         //alert(name);
         var desc = $("#taskdesc").val();
         //alert(desc);
         var due = $("#taskdue").val();
         //alert(due);
         var start = $("#taskstart").val();
         //alert(start);
         db.transaction(function(transaction) {
             var sql = "INSERT INTO list(name,description,due,start )VALUES(?,?,?,?)";
             transaction.executeSql(sql, [name, desc, due, start], function() {

                 alert("Values Inserted")
             }, error);
         });
     });

     function error(transaction, err)

     {
         alert("DB error : " + err.message);
         return false;
     }
 });

2 个答案:

答案 0 :(得分:0)

好的,所以我能够让这些命令在测试数据库上工作,所以我猜你要么有两个问题之一。你的任何一个值(name,desc,due,start)是未定义的,或者你需要添加一个';'到你的插入命令

var sql = "INSERT INTO list(name,description,due,start )VALUES(?,?,?,?);";

而不是

var sql = "INSERT INTO list(name,description,due,start )VALUES(?,?,?,?)";

答案 1 :(得分:0)

var x = {};
x.web = {};
db= null;
x.web.open=function()
{
    //alert("hiiii")
    db= openDatabase("amith",1.0,"To Do List Database",100*1024);
}
x.web.create=function()
{
    var data=db;
    //alert(data)
    data.transaction(function(transaction){
    transaction.executeSql(
    "CREATE TABLE IF NOT EXISTS type("+
    "ID INTEGER PRIMARY KEY ASC,"+
    " name TEXT NOT NULL,"+
    " description TEXT,"+
    " due DATETIME,"+
    " start DATETIME)");
    });

}
x.web.addTodo = function(name,desc,due,start) 
    {
        var data1 = db;
        data1.transaction(function(tx){
        var sql="INSERT INTO     
   type(name,description,due,start)VALUES(?,?,?,?)";
        tx.executeSql(sql,[name,desc,due,start],function(){
        //alert("Values Inserted")
        },error);
        });
    }
 function error (transaction, err) 

    {
        alert ("DB error : " + err.message);
        return false;
    }

function init()
{
    x.web.open();
    x.web.create();
}
 function addTodo() 
{
    //alert("toodod")
    var name = document.getElementById("taskname").value;
    var desc= document.getElementById("taskdesc").value;
    var due= document.getElementById("taskdue").value;
    var start=document.getElementById("taskstart").value
    //alert(name);
    x.web.addTodo(name,desc,due,start);
}