I'm developing a jQuery mobile application that needs to read and write to a Web SQL database. This is my first good-sized app that I have done on my own and I can't seem to figure out why it won't work. I've searched many forums, gone over a number of slides showing how to properly do it, but I still must be missing something.
Could someone please take a look at my code and point me in the right direction as to where I'm going wrong? This snippet is taken from a .js file (custom.js) that is imported into my .html file.
function enterNames() {
var collectionOfNames = document.getElementsByName("playerName");
var collectionOfColours = document.getElementsByName("playerColour");
for (var i = 0; i < numberOfPlayers; i++) {
var pName = "";
var pColour = "";
pName = collectionOfNames[i].value;
pColour = collectionOfColours[i].value;
golfDB.transaction(
function (transaction) {
transaction.executeSql("SELECT golferID FROM golfer WHERE golferName = ?;",
[pName],
function (tx, results) {
if (results.rows.length > 0) {
var aRow = results.rows.item(0);
playerID[i] = parseInt(aRow['golferID'].toString());
}
else {
golfDB.transaction(
function (transaction2) {
transaction2.executeSql(
"INSERT INTO golfer (golferName, colourID) VALUES (?,?);",
[pName, pColour],
function (tx1, results1) {
tx1.executeSql("SELECT golferID FROM golfer WHERE golferName = ?;",
[pName],
function (tx2, results2) {
var aRow2 = results.rows.item(0);
playerID[i] = parseInt(aRow2['golferID'].toString());
},
errorHandler);
},
errorHandler
);
}
);
}
},
errorHandler);
}
);
}
var dateTime = new Date();
golfDB.transaction(
function (transaction) {
transaction.executeSql("INSERT INTO game (numberOfPlayers,dateTime,gameCompleted) VALUES (?,?,?);", [numberOfPlayers, dateTime, GAME_IN_PROGRESS], null, errorHandler);
}
);
golfDB.transaction(
function (transaction) {
transaction.executeSql("SELECT gameID FROM game ORDER BY gameID DESC;", [dateTime], getGameID, errorHandler);
}
);
gameInProgress = true;
if (playing18holes) {
golfDB.transaction(
function(transaction) {
transaction.executeSql("SELECT holeID FROM hole WHERE courseID = ? OR courseID = ? ORDER BY holeID;", [currentCourseID, nextCourseNumber], getHoleIDs, errorHandler);
}
);
alert("Playing 18 holes");
}
else {
golfDB.transaction(
function(transaction) {
transaction.executeSql("SELECT holeID FROM hole WHERE courseID = ? ORDER BY holeID;", [currentCourseID], getHoleIDs, errorHandler);
}
);
alert("Playing 9 holes");
}
setHoleInformation();
}
I've been able to create the database
var golfDB = openDatabase('golfDB', '1.0', 'golf DB', 100 * 1024);
Create tables, Insert into those tables:
function runSqlQuery(sqlString, callback) {
golfDB.transaction(
function (transaction) {
transaction.executeSql(sqlString, [], callback, errorHandler);
}
);
}
function createTables() {
runSqlQuery("CREATE TABLE IF NOT EXISTS colour ( colourID INTEGER, colourName VARCHAR(45));", null);
runSqlQuery("INSERT INTO colour (colourID,colourName) SELECT 1, 'gold' UNION SELECT 2,'blue' UNION SELECT 3,'white' UNION SELECT 4,'red';", null); }
but for some reason I can't get the nested queries to work. Any ideas?