In this example, string.format()
is used to replace placeholders (like %s
for string or %d
for ) 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 = %s AND Gender = %s"
local lastName = conn:quote("O'Reilly")
local gender = conn:quote("Male")
-- Use string.format to replace the placeholders with actual values
local query = queryString:format(lastName, gender)
local result = conn:query{sql=query}
The query will look like:
SELECT * FROM Patients WHERE LastName = 'O''Reilly' AND Gender = 'Male'