Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In the Translator, you can read data from a database by either calling stored procedures or using conn:query

...

{} to execute

...

ad hoc SQL SELECT

...

statements on a database. See Writing SQL Statements.

Code Block
languagelua
local R = conn:query{sql='SELECT * FROM Patients WHERE LastName=' ..conn:quote(lastName)}

The conn:query{}

...

example of query and annotation results

...

Link to “writing SQL statements“

Can also call stored procedures (link to page) method prevents the accidental use of SQL statements that might modify the database from the Translator, which automatically executes when you make script changes or sample data changes.

Expand
titleAccessing Database Query Results

Using the Annotations you can view database query result tables easily:

Image Added

The function returns the database query result sets and a Lua table of all result sets. A for loop can be used to iterate through the results table and access values using :nodeValue().

Image Added
Expand
titleHandling Database Errors

conn:query{} can also throw a Lua error if a database error occurs. The error thrown is a table with two values:

  • message: a string with the description of the error.

  • code: an integer error code returned by the database.

pcall() can be used to perform a protected call, capturing any returned database errors allowing you to appropriately handle errors according to your needs. In this example, we are using pcall to catch and log the error message.

Image Added