0 Replies - 2964 Views - Last Post: 26 November 2012 - 09:36 PM

#1 AngieLeigh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-December 11

Javascript reading/writing to Sqlite in mobile app

Posted 26 November 2012 - 09:36 PM

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();
    // insert into database
    golfDB.transaction(
        function (transaction) {
            transaction.executeSql("INSERT INTO game (numberOfPlayers,dateTime,gameCompleted) VALUES (?,?,?);", [numberOfPlayers, dateTime, GAME_IN_PROGRESS], null, errorHandler);
        }
    );
    // get gameID
    golfDB.transaction(
        function (transaction) {
            transaction.executeSql("SELECT gameID FROM game ORDER BY gameID DESC;", [dateTime], getGameID, errorHandler);
        }
    );
    gameInProgress = true;
    // get holeID
    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");
    }
    // populate the game table
    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?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1