我需要在iOS的phonegap应用程序中使用sqlite,我在网上找到了这个工作样本。但是当我将它集成到我的应用程序时,当我单击添加记录或刷新按钮时,没有任何反应。数据库未更新,未添加任何数据。该示例说要添加query.min.js以使代码起作用,这就是我所做的。
Javascript代码:
var db;
var shortName = 'WebSqlDB';
var version = '1.0';
var displayName = 'WebSqlDB';
var maxSize = 65535;
function errorHandler(transaction, error) {
alert('Error: ' + error.message + ' code: ' + error.code);
}
function successCallBack() {
alert("DEBUGGING: success");
}
function nullHandler(){};
function onBodyLoad(){
alert("DEBUGGING: we are in the onBodyLoad() function");
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
db = openDatabase(shortName, version, displayName,maxSize);
db.transaction(function(tx){
//tx.executeSql( 'DROP TABLE User',nullHandler,nullHandler);
tx.executeSql( 'CREATE TABLE IF NOT EXISTS User(UserId INTEGER NOT NULL PRIMARY KEY, FirstName TEXT NOT NULL, LastName TEXT NOT NULL)',[],nullHandler,errorHandler);},errorHandler,successCallBack);
}
function ListDBValues() {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
$('#lbUsers').html('');
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM User;', [],
function(transaction, result) {
if (result != null && result.rows != null) {
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
$('#lbUsers').append('<br>' + row.UserId + '. ' + row.FirstName+ ' ' + row.LastName);} } },errorHandler); },errorHandler,nullHandler);
return;
}
function AddValueToDB() {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO User(FirstName, LastName) VALUES (?,?)',[$('#txFirstName').val(), $('#txLastName').val()], nullHandler,errorHandler); });
ListDBValues();
return false;
}
HTML:
<body onLoad="onBodyLoad()">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<input id="txFirstName" type="text" placeholder="FirstName">
<input id="txLastName" type="text" placeholder="Last Name">
<input type="button" value="Add record" onClick="AddValueToDB()">
<input type="button" value="Refresh" onClick="ListDBValues()"> <br>
<br>
<span style="font-weight:bold;">Currently stored values:</span>
<span id="lbUsers"></span>
</body>