AgileApps Support Wiki Pre Release

Difference between revisions of "ExecSQL"

From AgileApps Support Wiki
imported>Aeric
(Created page with "{{DISPLAYTITLE:execSQL}} Execute a SQL query.<noinclude> __TOC__</noinclude> ====Syntax==== :<syntaxhighlight lang="java" enclose="div"> Result result = Functions.execSQL (String…")
 
imported>Aeric
 
(14 intermediate revisions by the same user not shown)
Line 4: Line 4:
====Syntax====
====Syntax====
:<syntaxhighlight lang="java" enclose="div">
:<syntaxhighlight lang="java" enclose="div">
Result result = Functions.execSQL (String query} );
Result result = Functions.execSQL(String query);
</syntaxhighlight>
</syntaxhighlight>


Line 12: Line 12:


====Returns====
====Returns====
:[[Result Class|<tt>Result</tt>]] object
:[[Result Class|<tt>Result</tt>]] object. If the return code is greater than zero, use the [[Result_Class#getIterator]] method to cycle through the list of [[Parameters Class|Parameters]] objects it contains, one per record.
 
====Sample Code====
This sample 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";
      Logger.info("Sample:\n" + result.getMessage(), "SQL");
  }
  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");
      Logger.info("Sample: latest value = " + latest_value, "SQL");       
  }
} catch (Exception e) {
  String msg = "Sample: Exception during SQL search";
  Logger.info("Sample:\n" + e.getMessage(), "SQL");       
}
</syntaxhighlight>
 
====Sample App====
:* [[HowTo:Use a SQL Query to List Records in a Custom Page]]
:: This sample uses the execSQL operation to populate a JSP page with a list of object records.


====Learn More====
====Learn More====
:* [SQL Browser]]
:* [[SQL Browser]]
:* [[SQL Syntax]]
:* [[SQL Functions]]

Latest revision as of 23:43, 2 May 2014

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 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";
     Logger.info("Sample:\n" + result.getMessage(), "SQL");
  }
  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");
     Logger.info("Sample: latest value = " + latest_value, "SQL");        
  }

} catch (Exception e) {

  String msg = "Sample: Exception during SQL search";
  Logger.info("Sample:\n" + e.getMessage(), "SQL");        

} </syntaxhighlight>

Sample App

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

Learn More