SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Web SQL Database storing user settings - inserting and query issues!

    Im building an app with Cordova/phonegap and im using Web SQL as my database engine. The database will hold an id for the user, county, gender, age and a multiple choice.

    But my lack of SQL skills, have given me some trouble. Ive managed to create the database and table with these lines:

    PHP Code:
    onDeviceReady: function() {
        
    app.db window.openDatabase("Database""1.0""My user DB"200000);
    }, 
    Then i have a testIfDatabaseExists() function to see if should create Database or make a query. if create (im also inserting 2 test users into the database):

    PHP Code:
    createDatabase: function() {
        
    app.db.transaction(app.populateDBapp.populateErrorapp.populateSuccess);
    },

    populateDB: function(tx) {
        
    tx.executeSql('DROP TABLE IF EXISTS USERS');
        
    tx.executeSql('CREATE TABLE IF NOT EXISTS USERS (id unique, poll, county, gender, age)');
        
    tx.executeSql('INSERT INTO USERS (id, poll, county, gender, age) VALUES (1, "V", "Fyn", "Male", 25)');
        
    tx.executeSql('INSERT INTO USERS (id, poll, county, gender, age) VALUES (2, "S", "KBH", "Male", 35)');
    }, 
    From here Im having trouble with inserting the data inputs from the app user and also update the old user inputs with its old settings with the database query. Right now im just executing the query out into html with innerHTML to check if the query works. Ive tried having a onClick event on my submit button: AddValueToDB() - but i cant seem to make it do anything :/

    PHP Code:
    AddValueToDB: function() {
     if (!
    window.openDatabase) {
         
    alert('Databases are not supported in this browser.');
         return;
         }

     
    tx.executeSql('INSERT INTO USERS(id, poll, county, gender, age) VALUES (?,?,?,?,?)',[$('#txId').val(), $('#txPoll').val(), $('#txCounty').val(), $('#txGender').val(), $('#txAge').val()], app.populateError);
     }, 
    My HTML form:

    HTML Code:
    <form>
        <label for="id">Username/ID:</label>
        <input type="number" name="ID" id="txId" value="" />
        <label for="county">County:</label>
        <input id="txCounty" type="text" name="county" id="county" value="" placeholder="County" />
        <fieldset data-role="controlgroup" data-type="horizontal" id="txGender">
             <legend>Gender:</legend>
             <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2a" value="male" checked="checked">
             <label for="radio-choice-h-2a">Male</label>
             <input type="radio" name="radio-choice-h-2" id="radio-choice-h-2b" value="female">
             <label for="radio-choice-h-2b">Female</label>
        </fieldset>
        <label for="age">Age:</label>
        <input type="number" name="age" id="txAge" value="" placeholder="Age" />
    
        <input type="button" value="Add record" onClick="AddValueToDB()">
        <input type="button" value="Refresh" onClick="ListDBValues()">
    </form>
    I COULD REALLY NEED SOME GUIDANCE HERE !

  2. #2
    SitePoint Member
    Join Date
    May 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OPDATE: What I need some help with, is updating the row for the user, when the settings are changed:county, gender, age.. - my AddValueToDB() function doesnt work.
    Also outputting the user values into the input fields, I could use some guidance for!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •