Difference between revisions of "ExecSQL"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 41: Line 41:
   }
   }
} catch (Exception e) {
} catch (Exception e) {
   String msg = project + ": Exception during SQL search";
   String msg = "Sample: Exception during SQL search";
   Functions.debug("Sample:\n" + e.getMessage());         
   Functions.debug("Sample:\n" + e.getMessage());         
}
}

Revision as of 20:26, 8 February 2012

Execute a SQL query.

Syntax

<syntaxhighlight lang="java" enclose="div">

Result result = Functions.execSQL(String query); </syntaxhighlight>

Parameters

query
The SQL query to execute.
Learn more: SQL Syntax

Returns

Result object. If the return code is greater than zero, use the Result_Class#getIterator method to cycle through the list of Parameters objects it contains, one per record.

Sample Code

This sample code is a template for query processing. It retrieves a value from the most recent record that matches the specified criteria:

<syntaxhighlight lang="java" enclose="div">

try {

  String latest_value;
  String sql = 
     "SELECT some_field FROM MyObject " +
     "WHERE another_field = '" + someValue + "' " +
     "ORDER BY date_created DESC " +            
     "LIMIT 1";
  Result result = Functions.execSQL(sql);
  int resultCode = result.getCode();
  if (resultCode < 0)
  {
     // Error occurred
     String msg = "Sample: Error during SQL search";
     Functions.debug("Sample:\n" + result.getMessage());
  }
  else if (resultCode > 0)
  {
     // A record was found. (Otherwise, resultCode == 0)                     
     ParametersIterator it = result.getIterator();
     Parameters params = it.next();  // Use a loop if Limit > 1       
     latest_value = params.get("some_field");
     Functions.debug("Sample: latest value = " + latest_value);        
  }

} catch (Exception e) {

  String msg = "Sample: Exception during SQL search";
  Functions.debug("Sample:\n" + e.getMessage());        

} </syntaxhighlight>

Sample App

This sample uses the execSQL operation to populate a JSP page with a list of object records.

Learn More