在javascript中插入SQLite数据库时出现问题
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)');
})
它创建了表,但当我尝试插入一条记录时,我遇到了以下问题未捕获引用错误:没有定义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")
});
});
相关文章:
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- Ajax发布到PHP脚本,每5秒查询一次MySQL数据库的性能/问题
- 将捕获的图像插入数据库问题(Cordova/SQlite/JavaScript)
- 使用数据库中的图像填充dropzone.js删除图像的问题
- PhoneGap 中的数据库条目出现问题
- 在调用 ajax 和查询数据库时遇到问题
- 通过node-js脚本将文档数组插入mongodb中的数据库时出现问题
- 使用 sequelize ORM 连接到节点上的 Amazon RDS Postgres 数据库.js出现问题
- jquery下拉列表和发布到SQL数据库问题
- 根据类别从数据库中调用不同类型的问题
- 电影数据库“访问控制允许来源”问题
- 使用html、php、js从数据库中删除行时出现问题
- 可排序数据和更新数据库问题
- 在javascript中插入SQLite数据库时出现问题
- 在不处理数据库的情况下解决相同的问题
- couchDB数据库根问题
- 角度拖放保存在数据库中的位置和屏幕大小调整问题
- 与数据库的连接以及与输入处理有关的离子问题
- 通过javascript函数从数据库获取php数据时出现问题
- 在原生脚本应用程序中显示sqlite数据库数据有问题