我这里有一个工作的jquery-mobile代码,可以创建一个SQLite数据库,并在每次启动应用程序时自动输入。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css"></link>
<link rel="stylesheet" type="text/css" href="css/jquery.mobile.simpledialog.min.css" />
<script type="text/javascript" href="cordova-2.1.0.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile.simpledialog2.min.js"></script>
<script type="text/javascript" charset="utf-8">
var db = "";
db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 200000); //will create database Dummy_DB or open it
db.transaction(populateDB, errorCB, successCB);
//create table and insert some record
function populateDB(tx) {
//tx.executeSql('DROP TABLE IF EXISTS SoccerPlayer');
tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)');
tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")');
tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")');
}
//function will be called when an error occurred
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
//function will be called when process succeed
function successCB() {
alert("success!");
db.transaction(queryDB,errorCB);
}
//select all from SoccerPlayer
function queryDB(tx){
tx.executeSql('SELECT * FROM SoccerPlayer',[],querySuccess,errorCB);
}
function querySuccess(tx,result){
var playerlist = document.getElementById("SoccerPlayerList");
var players = "";
alert("The show is on");
var len = result.rows.length;
for (var i=0; i<len; i++){
alert(result.rows.item(i).Name + result.rows.item(i).Club);
players = players + '<li><a href="#"><p class="record">'+result.rows.item(i).Name+'</p><p class="small">Club '+result.rows.item(i).Club+'</p></a></li>';
}
playerlist.innerHTML = players;
$("#SoccerPlayerList").listview("refresh");
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="header" data-position="fixed" data-theme="b">
<h1>Soccer Player</h1>
</div>
<div data-role="content">
<ul id="SoccerPlayerList">
</ul>
</div>
</div>
<!--end of Soccer Player Page--->
</body>
</html>
我的问题是我无法在DDMS下的文件资源管理器中找到数据库文件本身。我已经读过它位于数据/数据//数据库下但它只包含以下内容:
数据库Dummy_DB应该在那里。有什么不对?我确信Dummy_DB及其内容确实存在,因为我一遍又一遍地启动应用程序,每次启动应用程序时listview内容都会继续填充。
以下是在模拟器中启动和关闭应用程序4次后的输出:
我现在感到很困惑,因为这是我第一次使用jquery-mobile学习SQLite。任何帮助都会很乐意接受,提前谢谢。
我使用eclipse ADT作为我的模拟器。
答案 0 :(得分:0)
写下
db = window.sqlitePlugin.openDatabase({“name”:“Dummy_DB”});
而不是
db = window.openDatabase(“Dummy_DB”,“1.0”,“Just a Dummy DB”,200000);