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{} method will execute SELECT statements when you

...

make any script alterations or

...

changes to the sample data. It assumes that the SELECT statements do not alter the state of the database and can be safely run to power the Translator’s annotation and auto-completion

...

example of query and annotation results

...

Link to “writing SQL statements“

Can also call stored procedures.

The function returns the database query result sets. It 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.

Code Block
languagelua
function getPatient(conn, lastName)
   -- Query patient from the database 
   local R = conn:query{sql='SELECT * FROM Patients WHERE LastName=' ..conn:quote(lastName)}
end 

function main(Data)
   -- Connect to the database 
   local conn = db.connect{api = db.SQLITE, name = 'demo.db'}

   local lastName = "O'Reilly"

   local Success, Err = pcall(getPatient, conn, lastName)
   if (not Success) then
      -- Handle error
      iguana.log(Err.message)
   end
end

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

...