Search code examples
google-apps-scriptgoogle-cloud-sql

Error: Resultset does not have a column 1


Using the JDBC services to connect to a Google Cloud SQL and
following the example of the overview everything works as expected.

When I try to do some other SQL statement like select database();
I get the error : Resultset does not have a column 1

This is the code I am using.

var sqlStatement = "select database();";
var pstmt = conn.prepareStatement(sqlStatement);
var rs = pstmt.executeQuery(sqlStatement);  
app.add(app.createLabel(rs.getString(1)));

I also tried with getObject() but it does get any better .

So any Idea, tutorials or examples how to deal with sqlStatements
different than the tipical select like use database or
select database()?


Solution

  • You are missing the statement to start the iterator - this is the .next() call on the record set. Here is an function that works for me. I am just logging the DB name here using Logger, but same should work for UiApp as well.

    function simpleRead() {
      var conn = Jdbc.getCloudSqlConnection("jdbc:google:rdbms://arunnagarajan:...");
      var stmt = conn.createStatement();
      var rs = stmt.executeQuery("select database()");
      rs.next(); //if you expect multiple rows, then do this in while(rs.next()) loop
      Logger.log(rs.getString(1));
      rs.close();
      stmt.close();
      conn.close();
    }