From d3ca4e2562fa40a5b3498a99a48e61f2e755f1d9 Mon Sep 17 00:00:00 2001 From: Jury Verrigni Date: Tue, 10 Dec 2019 23:07:52 +0000 Subject: [PATCH] Add sqlite sample --- simple-sqlite/.gitignore | 1 + simple-sqlite/ext/Server/__init__.lua | 65 +++++++++++++++++++++++++++ simple-sqlite/mod.json | 10 +++++ 3 files changed, 76 insertions(+) create mode 100644 simple-sqlite/.gitignore create mode 100644 simple-sqlite/ext/Server/__init__.lua create mode 100644 simple-sqlite/mod.json diff --git a/simple-sqlite/.gitignore b/simple-sqlite/.gitignore new file mode 100644 index 0000000..2fd7397 --- /dev/null +++ b/simple-sqlite/.gitignore @@ -0,0 +1 @@ +mod.db \ No newline at end of file diff --git a/simple-sqlite/ext/Server/__init__.lua b/simple-sqlite/ext/Server/__init__.lua new file mode 100644 index 0000000..5ca9916 --- /dev/null +++ b/simple-sqlite/ext/Server/__init__.lua @@ -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) diff --git a/simple-sqlite/mod.json b/simple-sqlite/mod.json new file mode 100644 index 0000000..38ef928 --- /dev/null +++ b/simple-sqlite/mod.json @@ -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" + } +}