在javascript中插入SQLite数据库的问题

时间:2015-12-21 12:06:41

标签: javascript sqlite

我正在用JS创建一个数据库

 var db = openDatabase('exampleDB', '1.0', 'Database', 2 * 1048 * 1048);
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF EXISTS alunos (ID INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, data TEXT)');
    })

它创建了表,但是当我尝试插入一条记录时,我遇到以下问题未捕获的ReferenceError:nome未定义

$(document).ready(function () {


$("#btnVolta").click(function () {
    window.location.href = 'index.html'
});
$("#procura").click(function () {
    var id = document.getElementById('idaluno').value;
    var nome = document.getElementById('nomealuno').value;
    var data = document.getElementById('dataluno').value;


    var posting = '{"ID":' + id + ',"Nome":"' + nome + '","DataNascimento":"'+ data + 'T00:00:00"}';
    post = JSON.parse(posting);

    alert(posting);
    readPosts(posting);
});


function readPosts(formData) {
    console.log('A inserir');

    $.ajax({
        url: "http://myserver/api/alunos/" ,
        type: "POST" ,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: formData,
        success: function (data) {
            //data - response from server
            $('#posts').html('Inserido com sucesso');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (navigator.onLine)
            {
                $('#posts').html('Erro' + jqXHR + textStatus + errorThrown);
            }else
            {
                db.transaction(function (tx) {
                    tx.executeSql('INSERT INTO alunos (nome, data) VALUES(?,?)',[nome,data]);
                })
                $('#posts').html('Inserido no DB Local');
            }
        }
    });
}

});

问题出在哪里?

我必须添加SQLite插件吗?如果是,我该怎么做?

谢谢大家

1 个答案:

答案 0 :(得分:0)

您尚未声明nome变量。

<强>更新

你没有声明变量。这就是为什么它不起作用。请尝试下面的代码

<script>
    $(document).ready(function ()
    {

        $("#btnVolta").click(function()
        {
            window.location.href = 'index.html'
        });
        $("#procura").click(function()
        {
            var id = document.getElementById('idaluno').value;
            var nome = document.getElementById('nomealuno').value;
            var data = document.getElementById('dataluno').value;

            var posting = '{"ID":' + id + ',"Nome":"' + nome + '","DataNascimento":"' + data + 'T00:00:00"}';
            post = JSON.parse(posting);

            alert(posting);
            readPosts(posting);
        });

        function readPosts(formData)
        {
            console.log('A inserir');

            $.ajax({
                url : "http://myserver/api/alunos/",
                type : "POST",
                dataType : "json",
                contentType : "application/json; charset=utf-8",
                data : formData,
                success : function(data)
                {
                    //data - response from server
                    $('#posts').html('Inserido com sucesso');
                },
                error : function(jqXHR, textStatus, errorThrown)
                {
                    if(navigator.onLine)
                    {
                        $('#posts').html('Erro' + jqXHR + textStatus + errorThrown);
                    }
                    else
                    {
                        db.transaction(function(tx)
                        {
                            var nome = "name goes here"; 
                            var data = "data goes here";
                            tx.executeSql('INSERT INTO alunos (nome, data) VALUES(?,?)', [nome, data]);
                        })
                        $('#posts').html('Inserido no DB Local');
                    }
                }
            });
        }
    });
</script>

更新2

使用此代码

db.transaction(function(tx)
                        {
                            var nome = "name goes here";
                            var data = "data goes here";
                            tx.executeSql('INSERT INTO alunos (nome, data) VALUES(?,?)', [nome, data], function(tran, success)
                            {
                                alert("in success")
                            }, function(tran, error)
                            {
                                console.log(error.message);
                                alert("in error")
                            });
                        });
相关问题