在javascript中插入SQLite数据库时出现问题
我正在用这种方式创建一个JS数据库在javascript中插入SQLite数据库时出现问题,javascript,sqlite,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)'); })
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)');
})
它创建了表,但当我尝试插入一条记录时,我遇到了以下问题未捕获引用错误:未定义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的插件?如果是,我怎么做
谢谢大家,你们还没有声明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>
我已经这样声明了
var nome=document.getElementById('nomealuno').value;var data=document.getElementById('dataluno').value代码>它解决了问题,但没有插入表,也没有错误,我需要一个sqlite插件?不,你不需要。它将删除navigator.onLine条件并重试。您似乎处于联机状态,因此它成功进入if条件。如果在Else条件中明显通过,则显示“Inserido no DB Local”已解决;)表中的不一致性感谢@KeyurSakaria
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")
});
});