Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sqlite sample #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simple-sqlite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod.db
65 changes: 65 additions & 0 deletions simple-sqlite/ext/Server/__init__.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- This simple mod demostrates how to interact with VEXT's integrated SQLite db.
-- Reference available at https://modders.link/wiki/doku.php?id=vext:ref:vu:lib:srv:sql

Events:Subscribe('Extension:Loaded', function()
-- Check the the output in the server's console.

-- Create and open a database at Mods/simple-sqlite/mod.db.
-- If for any reason this cannot be created/opened, Open() fails
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
end)
10 changes: 10 additions & 0 deletions simple-sqlite/mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Name": "simple-sqlite",
"Authors": [ "snaiperskaya" ],
"Description": "This simple mod demostrates how to interact with VEXT's integrated SQLite db.",
"Version": "1.0.0",
"HasVeniceEXT": true,
"Dependencies": {
"veniceext": "^1.0.0"
}
}