I have a problem displaying the results after executing a query in a SQLite database which is created on the run.
Here is my code
CREATING DATABASE
var mydb=false;
// initialise the database
initDB = function() {
try {
if (!window.openDatabase) {
alert('not supported');
} else {
var shortName = 'APP_DB';
var version = '0.1';
var displayName = 'It Happened Today DB';
var maxSize = 262144; // in bytes, 256kb
mydb = openDatabase(shortName, version, displayName, maxSize);
}
} catch(e) {
// Error handling code goes here.
if (e == INVALID_STATE_ERR) {
// Version number mismatch.
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
}
// db error handler - prevents the rest of the transaction going ahead on failure
errorHandler = function (transaction, error) {
// returns true to rollback the transaction
return true;
}
// null db data handler
nullDataHandler = function (transaction, results) { }
CREATING TABLE AND INSERTING VALUES
// create tables for the database
createTables = function() {
try {
mydb.transaction(
function(transaction) {
transaction.executeSql('CREATE TABLE milestones (ID INT( 10 ) NOT NULL, Title VARCHAR( 50 ) DEFAULT NULL, mYear INT( 11 ) NOT NULL, mMonth INT( 11 ) DEFAULT NULL, mDay VARCHAR( 10 ) DEFAULT NULL, mText VARCHAR( 2000 ) NOT NULL, Theme1 VARCHAR( 50 ) DEFAULT NULL, Theme2 VARCHAR( 50 ) DEFAULT NULL, ImageURL VARCHAR( 50 ) DEFAULT NULL, PRIMARY KEY ( ID ));', [], nullDataHandler, errorHandler);
transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear], [mMonth], [mDay], [mText], [Theme1], [Theme2], [ImageURL]) VALUES (2, "Cotton Mather", 1721, 6, 26, "Following the recommendation of Rev. Cotton Mather, Dr. Zabdiel Boylston of Boston completes the first inoculation against smallpox in the U.S., injecting his own son and two of his slaves.", "HIAm", null, "6-26 Cotton Mather3g04597v.jpg");', [], nullDataHandler, errorHandler);
transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear], [mMonth], [mDay], [mText], [Theme1], [Theme2], [ImageURL]) VALUES (6, "New York Hospital", 1771, 6, 13, "New York Hospital, the second in the colonies after the Pennsylvania Hospital, receives a royal charter from King George III under the name Society of the Hospital in the City of New York in America, later changed to Society of New York Hospital.", "HIAm", null, "Default.png");', [], nullDataHandler, errorHandler);
});
} catch(e) {
/// alert(e.message);
return;
}
};
TEXT2HTML
milestonesDataHandler = function(transaction, results){
var html = "";
for(var i=0; i < results.rows.length; i++){
var row = results.rows.item(i);
html += '<li class="elist"> \
<a href="article.template.html?id=">'+row['id']+'data-transition="none"> \
<img src="img/">'+row['ImageURL']+'" height="70" width="70" /> \
<h4>'+row['Title']+'</h4> \
<p>'+ dateFormat($row['mYear']+' '+$row['mMonth']+' '+$row['mDay'])+'</p> \
</a> \
</li>';
}
}
EXECUTE QUERY
loadMilestones = function(){
try {
mydb.transaction(
function(transaction){
transaction.executeSql("SELECT * FROM milestones", [], milestonesDataHandler, errorHandler);
});
}
catch(e) {
alert(e.message);
}
}
AT THE END OF THE HTML DOCUMENT I DO:
<script>$("div.miles").html(milestonesDataHandler);</script>
Nothing gets display. When I go to the Chrome debugger it says: 131 Uncaught TypeError: Cannot read property 'length' for undefined.
This is referring to the 3rd line in the TEXT2HTML part. It looks like for some reason, the results variable is not being filled.
Would you please help me solve this problem.
Thanks
未调用createTables
,因此未在调用milestonesDataHandler
之前创建或填充该表以显示任何结果。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.