In this example, gsub
is used to replace placeholders (like %s
) in a SQL query template with user-provided values - useful when you want to insert dynamic content into a pre-defined template.
local queryString = "SELECT * FROM Patients WHERE LastName = $LASTNAME AND Gender = $GENDER"
local lastName = conn:quote("O'Reilly")
local gender = conn:quote("Male")
-- Use gsub to replace the placeholders with actual values
local query = queryString:gsub("%$LASTNAME", lastName):gsub("%$GENDER", gender)
local result = conn:query{sql=query}
The query will look like:
SELECT * FROM Patients WHERE LastName = 'O''Reilly' AND Gender = 'Male'