Versions Compared

Key

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

...

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

The conn:query{} method will execute SELECT statements prevents the accidental use of SQL statements that might modify the database from the Translator, which automatically executes when you make any script alterations changes 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.The function returns the database query result sets. It 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.

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:

...

Image Added