I have created a web sql using sqlite to create a database using javascript and I want the results to be displayed onto another html page inside the list items tag. How can I achieve this? I am using jquery and phonegap as I am developing a mobile app.
// Populate the database
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// 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 + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
//the data fr开发者_StackOverflow社区om here to the html page.
}
}
// Transaction error callback
//
function errorCB(err) {
console.log("Error processing SQL: "+err.code);
}
// Transaction success callback
//
function successCB() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
}
html page:
<div data-role="content" class="ui-content" role="main">
<div id="wrapper" >
<div id="scroller" >
<ul data-role="listview" data-theme="c" class="ui-btn-inset">
<li>would like to put it inside here</li>
</ul>
</div>
</div>
Is it possible to include a link with it? Also is Ajax needed for this?
Thanks.
var ul = document.getElementById("scroller").getElementsByTagName("UL")[0];
for (var i=0; i<len; i++){
var str = "Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data;
str = str.replace(/>/g, ">").replace(/</g, "<").replace(/'/g, "'").replace(/"/g, """); // entity encode
ul.innerHTML += "<li>" + str + "</li>";
}
Here's a quick solution. You just append LI tags to the inner HTML of the UL.
Here is a simplified example:
//Replace the title with the first row
document.title = ("Row = " + 0 + " ID = " + results.rows.item(0).id + " Data = " + results.rows.item(0).data);
//Append the rest of the rows to the title
for (var i=1; i<len; i++)
{
document.title += ("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
}
//Copy the result to the specified element
document.querySelector("#scroller li").innerHTML("<pre>"+document.title+"</pre>");
精彩评论