Garry's Mod Wiki

Revision Difference

sql#528042

<type name="sql" category="libraryfunc" is="library"> <summary>The SQL library allows you to access powerful database software included with Garry's Mod. It is the preferred and fastest method of storing large amounts of data. The database is located in sv.db serverside and cl.db clientside, both in the Garry's Mod base folder. SQL is a whole scripting language in itself although relatively simple, it's something you'll need to read up on before using this library. ⤶ For setup, complete steps:⤶ 1. Download the latest module for your server's operating system and architecture using the links from this page: https://github.com/FredyH/MySQLOO (most often required gmsv_mysqloo_win32.dll). 2. Download zip and copy catalog `lua` from archive to `GarrysMod/garrysmod`.⤶ 3. Open `addons/darkrpmodification/lua/darkrp_config` and set need parameters in `mysql.lua`.⤶ ⤶ Example usage:⤶ ⤶ <summary>The SQL library allows you to access powerful database software included with Garry's Mod. It is the preferred and fastest method of storing large amounts of data. The database is located in sv.db serverside and cl.db clientside, both in the Garry's Mod base folder. SQL is a whole scripting language in itself although relatively simple, it's something you'll need to read up on before using this library. ⤶ ⤶ Example of using sql. ``` require( "mysqloo" )⤶ ⤶ db = mysqloo.connect( "127.0.0.1", "username", "password", "DarkRP", 3306 ) ⤶ function db:onConnected()⤶ ⤶ print( "Database has connected!" ) ⤶ local q = self:query( "SELECT * FROM userinformation" )⤶ function q:onSuccess( data ) ⤶ print( "Query successful!" )⤶ PrintTable( data ) ⤶ end⤶ ⤶ function q:onError( err, sql )⤶ ⤶ print( "Query errored!" )⤶ print( "Query:", sql )⤶ print( "Error:", err )⤶ ⤶ end⤶ ⤶ q:start()⤶ ⤶ end⤶ ⤶ function db:onConnectionFailed( err )⤶ ⤶ print( "Connection to database failed!" )⤶ print( "Error:", err )⤶ ⤶ end⤶ ⤶ db:connect()⤶ -- Creating a table⤶ sql.Query("CREATE TABLE my_db_table( id NUMBER , name TEXT )" ) ⤶ -- Inserting a value to the table⤶ sql.Query("INSERT INTO my_db_table( id , name ) VALUES( 1 , 'First') ") ⤶ -- Printing the tables data⤶ PrintTable( sql.Query("SELECT * FROM my_db_table ") ) ⤶ -- Deleting the table⤶ sql.Query("DROP TABLE my_db_table") ``` ⤶ ⤶ Known GMod SQL settings:⤶ * No `ATTACH` and `DETACH` operations (`SQLITE_OMIT_ATTACH = 1`)⤶ * No Virtual Tables (`SQLITE_OMIT_VIRTUALTABLE = 1`)⤶ * Temp files are stored in memory (`SQLITE_TEMP_STORE = 3`)⤶ * No `VACUUM` operation</summary>⤶ ⤶ </summary>⤶ </type>