In the Translator, you can write data to a database by executing SQL statements using conn:execute{}
or by calling stored procedures. When writing to a database, you can perform INSERT, UPDATE, or DELETE operations with conn:execute{}
. As an alternative to conn:query{}
for reading data from a database, SELECT queries can also be executed through this method.
local values = {'John', 'Smith', '1980-01-01', 'Male'} local T = {} for i, v in ipairs(values) do T[i] = conn:quote(v) -- safely quote each value end local sqlInsert = 'INSERT INTO Patients (FirstName, LastName, DOB, Gender) VALUES ('..table.concat(T, ', ')..')' -- Execute SQL Insert local result = conn:execute{sql=sqlInsert, live=true}
In the example above conn:quote()
safely quotes string and date values and table.concat()
is used to concatenate the quoted values for the SQL statement. See Writing SQL Statements for additional tips on structuring SQL statements.
By default, SQL statements executed with conn:execute{}
are only run when the component is executed outside the Translator’s editing mode. To override this behavior and execute the statement during script editing, use the Live Flag to set live=true
. However, use this cautiously – each script change or use of sample data will trigger the SQL execution, potentially leading to multiple unintended INSERTs or UPDATEs.