[英]Problems inserting SQLite database in javascript
我用這種方式在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)');
})
它創建了表,但是當我嘗試插入一條記錄時,我遇到了以下問題Uncaught 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插件嗎?如果可以,該怎么辦?
多謝你們
您尚未聲明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")
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.