SQL
Summary
Methods
Methods
Open
Open(): bool
Returns
Close
Close()
Query
Query(query: string, …args: any): table | nil
Parameters
Name |
Type |
Description |
query |
string |
|
…args |
any |
|
Returns
Type |
Description |
table | nil |
|
Escape
Escape(text: string): string
Parameters
Name |
Type |
Description |
text |
string |
|
Returns
Blob
Blob(data: string): SQLBlob
Parameters
Name |
Type |
Description |
data |
string |
|
Returns
Error
Error(): string
Returns
AffectedRows
AffectedRows(): int
Returns
LastInsertId
LastInsertId(): int
Returns
Examples
if not SQL:Open() then
return
end
-- Create our table.
local query = [[
CREATE TABLE IF NOT EXISTS test_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text_value TEXT,
int_value INTEGER,
real_value REAL,
blob_value BLOB,
some_null_value BLOB,
not_null_text TEXT NOT NULL
)
]]
if not SQL:Query(query) then
print('Failed to execute query: ' .. SQL:Error())
return
end
-- Insert some test data.
query = 'INSERT INTO test_table (text_value, int_value, real_value, blob_value, some_null_value, not_null_text) VALUES (?, ?, ?, ?, ?, ?)'
if not SQL:Query(query, 'My Text', 1337, 420.69, SQL:Blob('My Blob'), nil, 'My Not Null Text') then
print('Failed to execute query: ' .. SQL:Error())
return
end
if not SQL:Query(query, 'My Text 2', 13372, 420.692, SQL:Blob('My Blob 2'), nil, 'My Not Null Text 2') then
print('Failed to execute query: ' .. SQL:Error())
return
end
print('Inserted data. Insert ID: ' .. tostring(SQL:LastInsertId()) .. '. Rows affected: ' .. tostring(SQL:AffectedRows()))
-- Test the NOT NULL constraint.
if not SQL:Query(query, 'My Text', 1337, 420.69, SQL:Blob('My Blob'), nil, nil) then
-- Error should be "NOT NULL constraint failed: test_table.not_null_text"
print('Failed to execute query: ' .. SQL:Error())
end
-- Fetch all rows from the table.
results = SQL:Query('SELECT * FROM test_table')
if not results then
print('Failed to execute query: ' .. SQL:Error())
return
end
-- Print the fetched rows.
for _, row in pairs(results) do
print('Got row:')
print(row)
end
Last modified April 27, 2020:
Add SQL example (381f84b1)