I am trying to delete a row in a database via external JS, but don’t think the syntax is right. What I have right now is my best guess as to what it should be.
The database rows show an ID, filename, and title (context: a bookmarked or favorite page), and when a query is made, it shows two buttons side by side for each row; the left button shows the title and when tapped goes to the page, while the right button is used to remove the row. This code successfully generates the buttons (via innerHtml):
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("BOOKMARKS table: " + len + " rows found.");
for (var i=0; i<len; i++) {
[COLOR="#FF0000"]rowId = results.rows.item(i).id;[/COLOR]
document.getElementById("output").innerHTML +="<div class='row'>" +
"<a href='" + results.rows.item(i).filename + "'><input type='button' class='buttonBM' value='" + results.rows.item(i).title + "'></a></div>" +
"<div class='buttonRemove'><input type='button' class='buttonEdit' value='Remove' onclick='removeRow('" + [COLOR="#FF0000"]rowId[/COLOR] + "')'></a></div>";
}
}
What I need to do is take the rowId and use it to delete the correct row in the DB, and I do it like this:
function removeRow() {
db = window.openDatabase("Database", "1.0", "Bookmarks", 200000);
[COLOR="#FF0000"]var rowId;[/COLOR]
db.transaction(function(tx) {
tx.executeSql("delete from BOOKMARKS where id='" + [COLOR="#FF0000"]rowId[/COLOR] + "'");
}, errorCB, successCB()); alert('Bookmark removed.');
}
However, it fails to delete the row. Google Tools gives the error as “Uncaught SyntaxError: Unexpected token }” which is on the external HTML page (which could be any page in the app containing the Add Bookmark button). The error points to the <HTML> line, so it could be anything.
Below is the above code in context:
// Add row via button on external page
function insertRow() {
db = window.openDatabase("Database", "1.0", "Bookmarks", 200000);
db.transaction(function(tx) {
var id, filename = window.location.pathname, title = document.title;
tx.executeSql("insert into BOOKMARKS(id, filename, title) values(?,?,?)", [id, filename, title]);
}, errorCB, successCB()); alert('Bookmark added.');
}
// Display DB results (onLoad)
function queryDB(tx) {
tx.executeSql("SELECT * FROM BOOKMARKS", [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
var len = results.rows.length;
console.log("BOOKMARKS table: " + len + " rows found.");
for (var i=0; i<len; i++) {
rowId = results.rows.item(i).id;
document.getElementById("output").innerHTML +="<div class='row'>" +
"<a href='" + results.rows.item(i).filename + "'><input type='button' class='buttonBM' value='" + results.rows.item(i).title + "'></a></div>" +
"<div class='buttonRemove'><input type='button' class='buttonEdit' value='Remove' onclick='removeRow('" + rowId + "')'></a></div>";
}
}
// Remove row via button on this page
function removeRow() {
db = window.openDatabase("Database", "1.0", "Bookmarks", 200000);
var rowId;
db.transaction(function(tx) {
tx.executeSql("delete from BOOKMARKS where id='" + rowId + "'");
}, errorCB, successCB()); alert('Bookmark removed.');
}
// Transaction error callback (corrected per Sitepoint.com post)
function errorCB(tx, err) {
alert('Error processing SQL: ' + err);
}
Of course, the Google error could be pointing to an entirely different part of the two pages!