gpt4 book ai didi

javascript - 在 Javascript 中从数据库中检索值

转载 作者:行者123 更新时间:2023-11-30 18:24:51 24 4
gpt4 key购买 nike

我是 PhoneGap 移动应用程序开发的新手。我创建了一个表格来添加和显示 SQLite 数据库中学生的姓名、地址和电话号码。但问题是我不知道如何检索和显示文本框中的值。

<!DOCTYPE HTML>
<html>
<head>
<title>Contact Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8">

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}


function populateDB(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (sname ,saddress ,sphone)');
}


function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}

// Transaction success callback
//
function successCB() {
alert("success!");
}

function add(tx){
var name=document.getElementById('n');
var address=document.getElementById('a');
var phone=document.getElementById('p');
tx.executeSql('INSERT INTO DEMO (sname ,saddress ,sphone) VALUES ('"+name+"','"+address+"','"+phone+"')');
//tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');

}

function show(tx){
var name=document.getElementById('n');
tx.executeSql('SELECT * FROM DEMO WHERE (sname='"+name+"')');
document.f.n.value=name;
document.f.a.value=//??;
document.f.p.value=//??;
}

</script>
</head>
<body>
<form name="f" method="get" action="">
Name :<input type="text" id="n" size="10"></input><br>
Add &nbsp;&nbsp;&nbsp; :<input type="text" id="a" size="10"></input><br>
Phone :<input type="text" id="p" size="10"></input><br>
<input type="button" value="Add" onClick="add()">
<input type="button" value="Show" onClick="show()">
</form>
</body>
</html>

最佳答案

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady()
{
alert("onDeviceReady called");
}


function populateDB(tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (sname ,saddress ,sphone)');
var name=document.getElementById('n');
var address=document.getElementById('a');
var phone=document.getElementById('p');
tx.executeSql('INSERT INTO DEMO (sname ,saddress ,sphone) VALUES ('"+name.value+"','"+address.value+"','"+phone.value+"')');
}


function errorCB(tx, err)
{
alert("Error processing SQL: "+err);
}

// Transaction success callback
//
function successCB()
{
alert("success!");
}

function add()
{
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}

有关更多信息,请参阅 link here

//show data from db

// Transaction success callback
function show()
{
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
}
// Query the database

function queryDB(tx)
{
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

// Query the success callback

function querySuccess(tx, results)
{
var len = results.rows.length;
console.log("DEMO table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " sname = " + results.rows.item(i).sname + " saddress = " + results.rows.item(i).saddress);
}

}

// Transaction error callback

function errorCB(err)
{
console.log("Error processing SQL: "+err.code);
}

关于javascript - 在 Javascript 中从数据库中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11150425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com