【发布时间】:2023-04-02 21:20:01
【问题描述】:
我做了一个程序,在sqlite数据库中插入和更新记录,并在Websql中查看数据库中的数据。
我创建了文本框字段,当我们输入数据时,单击提交按钮将其存储在数据库中。
我无法用在文本框中输入的新值更新数据库中的记录。
但是我可以在数据库中插入数据,但不能更新数据库中的值。
这是我的编码。
var updateStatement = "UPDATE login SET username=?,password=?,firstname = ?, lastname = ? ,hobby=?,email=? WHERE id=?";
var db=window.openDatabase('MYFORM','1.0','MYFORM',200000);
$(document).ready(function()
{
initDatabase();
$("#update").click(updateRecord);
});
function initDatabase() // Function Call When Page is ready.
{
try {
if (!window.openDatabase) // Check browser is supported SQLite or not.
{
alert('Databases are not supported in this browser.');
}
else {
showRecords();
}
}
catch (e) {
if (e == 2) {
// Version number mismatch.
console.log("Invalid database version.");
} else {
console.log("Unknown error " + e + ".");
}
return;
}
}
function updateRecord() // Get id of record . Function Call when Delete Button Click..
{
var usernamenew = $('input:text[id=username]').val().toString();
var passwordnew = $('input:password[id=pass]').val().toString();
var firstnamenew = $('input:text[id=firstname]').val().toString();
var lastnamenew = $('input:text[id=lastname]').val().toString();
var hobbynew = $('input:text[id=hobby]').val().toString();
var emailnew= $('input:text[id=email]').val().toString();
var useridnew = $("#id").val();
db.transaction(function (tx) { tx.executeSql(updateStatement, [usernamenew,passwordnew,firstnamenew, lastnamenew,hobbynew,emailnew,Number(useridnew)],loadAndReset, onError) });
}
HTML
<label for="username"> Username :</label>
<label for="pass"> Password :</label>
<input name="password" type="password" id="pass" placeholder="your password" size="20" maxlength="20" width="20" />
<label for="firstname"> Firstname :</label>
<input type="text" name="firstname" id="firstname" placeholder="your full name"/>
<label for="lastname"> Lastname :</label>
<input type="text" name="lastname" id="lastname" placeholder="your last name"/>
<label for="hobby"> Hobby : </label>
<input type="text" name="hobby" id="hobby" placeholder="your hobby" /><br ><br >
<label for="email"> Email :</label>
<input type="text" name="email" id="email" placeholder="Your email address"/><br >
<br >
<a href="#" class="ui-btn ui-btn-inline ui-corner-all ui-btn-b "id="update" type="submit" >Update</a>
更新问题
我还有一个名为 showRecords() 的函数,一旦我们调用该函数,它将以列表的形式显示所有记录,并且我在列表中有一个编辑按钮,如果我单击该编辑按钮,它应该加载特定的在编辑框中的下一页中记录..ie..
如果我们点击 id=1,它应该使用 id=1 的 loadRecords() 函数在下一页中的编辑框中加载记录,并且与其他相应的类似身份证。
我可以知道如何实现这个请帮助..
showRecords 函数的编码
function showRecords() // Function For Retrive data from Database Display records as list
{
$("#results").html('')
db.transaction(function (tx) {
tx.executeSql(selectAllStatement, [], function (tx, result) {
dataset = result.rows;
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
var linkeditdelete = '<ul><li>' + item['username'] + ' , ' + item['password'] + ' ,' +item['firstname']+' , ' + item['lastname'] +' , ' + item['hobby']+','+item['email']+ '<a href="page4.html" onclick="loadRecord(' + 'id' + ') id="edit";">edit</a>' +' ' + '<a href="#" onclick="deleteRecord(' + item['id'] + ');">delete</a></li></ul>';
$("#results").append(linkeditdelete);
}
});
});
}
function loadRecord(i)
{
var item = dataset.item(i);
$("#username").val((item['username']).toString());
$("#pass").val((item['password']).toString());
$("#firstname").val((item['firstname']).toString());
$("#lastname").val((item['lastname']).toString());
$("#hobby").val((item['hobby'])).toString();
$("#email").val((item['email']).toString());
$("#id").val((item['id']).toString());
}
【问题讨论】:
-
试试这样
var updateStatement = "UPDATE login SET username="+username+",password="+password+",firstname ="+firstname+", lastname = "+lastname+" ,hobby="+hobby+",email="+email+" WHERE id="+id; -
和你的sqlexecute就像
tx.executeSql("upadatequery,[],onsuccess); -
试过了还是没解决!
-
这个语法正确吗? //db.transaction(function (tx) { tx.executeSql("UPDATE login SET (username= '"+usernamenew+"' ,password='"+passwordnew+"',firstname='"+firstnamenew+"' ,lastname=' "+lastnamenew+"' ,hobby='"+hobbynew+"',email='"+emailnew+"') WHERE (id= '"+useridnew+"')");@Aravin
-
是的,我在表中使用的是正确的。
标签: html sqlite jquery-mobile