Versions Compared

Key

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

...

These techniques can be used when reading or writing to a database using SQL statements in the Translator.

Expand
titleString concatenation can be used for simple SQL statements.

Simple string concatenation can be used for simple SQL statements.

Code Block
local result = conn:query{sql='SELECT * FROM Patients WHERE LastName=' ..conn:quote(lastName)}
Expand
titletable.concat can be used to concatenate multiple values from a table into a SQL statement.

In this example, each value is escaped and then concatenated into the SQL statement.

Code Block
languagelua
local values = {'John', "O'Reilly", '1980-01-01', 'Male'} 
-- Loop through table of values
local T = {} 
for i, v in ipairs(values) do 
   T[i] = conn:quote(v) 
end 
   
-- Execute SQL Insert
local sqlInsert = 'INSERT INTO Patients (FirstName, LastName, DOB, Gender) VALUES ('..table.concat(T, ', ')..')' 
local results = conn:execute{sql=sqlInsert, live=true}

In the Annotations you can see:

  • The for loop using conn:quote to handle special characters.

  • The result of the SQL execution.

...