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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
Using the Annotations you can view database query result tables easily: 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 |
Expand | ||
---|---|---|
| ||
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 |
Code Block | ||
---|---|---|
| ||
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:
...