title: JSON, Javascript & Dynamically Created & Populated Tables
body: <p>I've got the following JSON structure that defines a table and it's data.</p>
  
  <pre>
  var arrTable = 
    [{"table": "tblConfig",
    "def":
      [{"column": "Property", "type": "TEXT NOT NULL"},
       {"column": "Value",    "type": "TEXT NOT NULL"}],
    "data":
      [{"Property": "VersionNumber", "Value": "1.0"},
       {"Property": "ReleaseDate",   "Value": "2010-01-01"}]
    }]</pre>
  
  <p>The code describes a table named "tblConfig" with two columns, "Property" and "Value" both are type "TEXT NOT NULL". The table has two rows of data.</p>
  
  <pre>Property_______Value_____
  VersionNumber  1.0
  ReleaseDate    2010-01-01</pre>
  
  <p>Below is my Javascript code to create and populate the table. It builds the create table SQL great but I'm having trouble with the populate function. </p>
  
  <pre>dbController.updateDatabase = function () {
    this.db.transaction(function (transaction) {
  //load data.json
      var dbDefs = dbController.jsonObject(url + "data.json")
  //parse array "table"
      eval(dbDefs);
      for (var i in arrTables) {
        createTable(arrTables[i].table, arrTables[i].def, arrTables[i].data);
      }
  //create table
      function createTable(table, arrDef, arrData) {
        var arrColumns = [];
        var strSQL = "CREATE TABLE " + table + " (";
        for (var j in arrDef) {
          arrColumns.push(arrDef[j].column);
          strSQL += arrDef[j].column + " " + arrDef[j].type + ", ";
        }
        strSQL = strSQL.substring(0, strSQL.lastIndexOf(",")) + ")";
        transaction.executeSql(strSQL, [],
          function () {
            console.log(table + " created.");
            populateTable(table, arrData);
            return;
          },
          dbController.errorHandler
        );
      }
  //populate table
      function populateTable(table, arrData) {
        ...
      }
    });
  };</pre>
  
  <p>I want to be able to get the column names ("Property" &amp; "Value") out of the "data" object and use them in the insert SQL string. I thought something like this would work. </p>
  
  <pre>//populate table
      function populateTable(table, arrColumns, arrData) {
        var strVal;
        var arrVal;
        for (var k in arrData) {
          strVal = "?,";
          <b>arrVal.push(arrData[k]. ~~ KEY VALUE NAME ~~ )</b>
        }
        var strSQL = "INSERT INTO " + table + " (" + strCol + ") ";
        strSQL += "VALUES (" + strVal.lastIndexOf(",") + ");";
        transaction.executeSql(strSQL, arrVal, null, dbController.errorHandler);
        console.log("  " + table + " populated.");
      }</pre>
  
  <p>The bugger is the <b>bolded</b> statement. I've tried using .hasOwnProperty() on arrData but that only returns 0 and 1, not the words "Property" and "Value". I don't want to use the "arrData[k].Property" notation because there are many more tables in the schema and I don't want to write create and insert statements for each table.   </p>
  
  <p>Any help is greatly appreciated.
  <hr>
  Thanks to everyone for their help and head-scratching due to my late-night post. My intention with this function was to have a JSON object that defined multiple tables and held the table's data. Since none of the tables have the same number of columns or column names I had to devise a way of getting the data out of "data" without using the <code>object.member</code> syntax. I had forgotten about the <code>object["member"]</code> syntax.<br />
  Here's the solution.
  <pre>dbController.updateDatabase = function (a) {
    this.db.transaction(function (transaction) {
  //load data.json
      var dbDefs = dbController.jsonObject(dbController.dbFolder + "data.json")
  </pre><pre>
  //parse array "table"
      eval(dbDefs);
      for(var i in arrTables) {
        createTable(arrTables[i].table, arrTables[i].def, arrTables[i].data);
      }
  </pre><pre>
  //create table
      function createTable(table, arrDef, arrData){
        var arrColumns = [];
        var strSQL = "Create Table " + table + " (";
        for(var j in arrDef) {
          arrColumns.push(arrDef[j].column);
          strSQL += arrDef[j].column + " " + arrDef[j].type + ", ";
        }
        strSQL = strSQL.substring(0, strSQL.lastIndexOf(",")) + ")";
        transaction.executeSql(strSQL, [],
          function() {
            console.log(table + " created.");
            populateTable(table, arrColumns, arrData);
            return;
          },
          dbController.errorHandler
        );
      }
  </pre><pre>
  //populate table
      function populateTable(table, arrColumns, arrData) {
        for (var k in arrData) {
          var arrVal = [];
          var strVal = "";
          for (var l = 0; l &lt; arrColumns.length; l++) {
            strVal += "?,";
            arrVal.push(arrData[k][arrColumns[l]])
          }
          var strSQL = "Insert Into "+table+" (" + arrColumns.toString()+") ";
          strSQL += "Values ("+strVal.substring(0, strVal.lastIndexOf(","))+");";
          transaction.executeSql(strSQL, arrVal, null, dbController.errorHandler);
        }
        console.log("  " + table + " populated.");
      }
    });
  };
  </pre></p>