Squirrel/Server/Functions/SQLite/sqlite query
From Liberty Unleashed Wiki
(Difference between revisions)
(doh) |
m (Added return 1;) |
||
(One intermediate revision by one user not shown) | |||
Line 14: | Line 14: | ||
== Example == | == Example == | ||
− | + | This will carry out the required query when 'quey' is entered into the server console. | |
− | + | ||
<code lang="squirrel"> | <code lang="squirrel"> | ||
− | - | + | function onScriptLoad() |
+ | { | ||
+ | LoadModule( "lu_sqlite" ); | ||
+ | pSQDatabase <- sqlite_open( "database.sqlite" ); | ||
+ | |||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | function onConsoleInput( szCommand, szText ) | ||
+ | { | ||
+ | if ( szCommand == "query" ) | ||
+ | { | ||
+ | local query = sqlite_query( pSQDatabase, "SELECT Foo FROM Bar WHERE FooBar='" + szText + "'" ); | ||
+ | } | ||
+ | |||
+ | return 1; | ||
+ | } | ||
</code> | </code> | ||
=== Notes === | === Notes === | ||
− | + | The function [[Squirrel/Server/Functions/Scripts/LoadModule|LoadModule]] and [[Squirrel/Server/Functions/SQLite/sqlite_open|sqlite_open]] and event [[Squirrel/Server/Events/Misc/onScriptLoad|onScriptLoad]] and [[Squirrel/Server/Events/Misc/onConsoleInput|onConsoleInput]] were used in this example. More info about them in the corresponding pages. | |
For a larger example, check the [[Squirrel/Examples/SQLite|SQLite database example]] from the wiki tutorials. | For a larger example, check the [[Squirrel/Examples/SQLite|SQLite database example]] from the wiki tutorials. |
Latest revision as of 16:52, 25 October 2010
Note: This function requires the external module lu_sqlite.
This function executes a query to a SQLite database. The function returns a pointer to a result struct or null if the query does not return any values. The query result should always be freed using sqlite_free to avoid any memory leaks.
[edit] Syntax
UserPointer sqlite_query( UserPointer database, string query )
[edit] Arguments
- database - This is a pointer to a previously opened database
- query - This is the query to be executed in SQL language
[edit] Example
This will carry out the required query when 'quey' is entered into the server console.
function onScriptLoad() { LoadModule( "lu_sqlite" ); pSQDatabase <- sqlite_open( "database.sqlite" ); return 1; } function onConsoleInput( szCommand, szText ) { if ( szCommand == "query" ) { local query = sqlite_query( pSQDatabase, "SELECT Foo FROM Bar WHERE FooBar='" + szText + "'" ); } return 1; }
[edit] Notes
The function LoadModule and sqlite_open and event onScriptLoad and onConsoleInput were used in this example. More info about them in the corresponding pages.
For a larger example, check the SQLite database example from the wiki tutorials.
[edit] Related Functions
These functions are provided by the official module lu_sqlite.