title: Creating dynamic Prepared Statements to insert into database
body: <p>I'm trying to write one row of data into a Google Cloud SQL database, using a prepared statement as described here:</p>
  
  <p><a href="https://developers.google.com/apps-script/guides/jdbc" rel="nofollow">https://developers.google.com/apps-script/guides/jdbc</a></p>
  
  <p>I have the data stored in an object called valuesByTitle, which looks like this:</p>
  
  <p>{NAME: 'fun event', LOCATION: '123 Main St.'}</p>
  
  <p>The data is inserted into the SQL table if I write everything out like this:</p>
  
  <pre><code>var conn = getConnection(); // connects to db
  var stmt = conn.prepareStatement('INSERT INTO events '
    + '(NAME, LOCATION) values (?, ?)');
  stmt.setString(1, valuesByTitle['NAME']);
  stmt.setString(2, valuesByTitle['LOCATION']);
  stmt.execute();
  }
  </code></pre>
  
  <p>However, I would like to automate the process, so I don't have to change the code each time the variables change. </p>
  
  <p>I have two new variables, one for the column names and one for a set of place holders (e.g. "?") for the insert statement. </p>
  
  <pre><code>var columns = Object.keys(valuesByTitle);
  var placeholders = columns.map(function(column) { 
  return "?";
  };
  </code></pre>
  
  <p>Using these, this prepared statement should automatically insert whatever is in the valuesByTitle object (assuming all the values are string values):</p>
  
  <pre><code>var conn = getConnection();  
  var stmt = conn.prepareStatement("INSERT INTO events (" + columns.join(",") + ") VALUES (" + placeholders.join(",") + ")");
  for (i = 0; i &lt; columns.length; i++) {
  stmt.setString(i+1, valuesByTitle[columns[i]]);  
  }
  stmt.execute();
  </code></pre>
  
  <p>For some reason, it's not inserting the data. Surprised not to find any examples either. Is the logic off or is it just not possible to do?</p>
  
  <p>Thanks.</p>