Sidebar

How do I use qie.getDbConnection()?

0 votes
629 views
asked Dec 26, 2014 by mike-r-7535 (13,830 points)
I need to read a blob data from a database.  How do I get the raw value since doQuery will convert it to a String.

2 Answers

0 votes
 
Best answer

This example will show you how to run queries, then process the result as if it was done with a qie.doQuery()

 

function executeQuery(connectionName, queryString) {
  //NOTE: This does not return a header row.
  //NOTE: Fields can only be accessed via index. Not by name.

 

   var pQuery = qie.getParameterizedQuery(queryString);
   return pQuery.doQuery(connectionName);
}

answered Dec 26, 2014 by ben-s-7515 (12,640 points)
edited Jun 26, 2019 by brandon-w-8204
0 votes
Here is an example of how to get the DATA value from MY_TABLE, which happens to be a BLOB.
 
var query = "select DATA\n" +
"  from MY_TABLE\n" +
" where MY_TABLE_ID = 2";  // TODO: insert your record identifier.
 
var dbCDA = "";
var connection = qie.getDbConnection(dbName);
var preparedStatement;
try {
   // We have to prepare the query to be executed, and get a handle on that query
   preparedStatement = connection.prepareStatement(query);
   var resultSet = preparedStatement.executeQuery();
   
   while (resultSet.next()) {
      var inputStream = resultSet.getBinaryStream("DATA");
      dbCDA += IOUtils.toString(inputStream, "UTF-8");
   }
 
} finally {
   // close the connection to avoid connection leaks
   try {
      if (preparedStatement !== null) {
         preparedStatement.close();
      }
   } catch (err) {
      qie.info("error closing the preparedStatement " + err);
   }
   connection.close();
}
 
answered Dec 26, 2014 by mike-r-7535 (13,830 points)
reshown Dec 26, 2014 by ben-s-7515
...