Garry's Mod Wiki

Revision Difference

sql.Query#529365

<function name="Query" parent="sql" type="libraryfunc"> <description>Performs a query on the local SQLite database, returns a table as result set, nil if result is empty and false on error.</description> <realm>Shared and Menu</realm> <args> <arg name="query" type="string">The query to execute.</arg> </args> <rets> <ret name="" type="table">false is returned if there is an error, nil if the query returned no data.</ret> </rets> </function> <example> <description>Functions that are examples of saving and creating information into the database.</description> <code> function CreateTable() sql.Query("CREATE TABLE player_data(SteamID TEXT, Money INTEGER)") sql.Query( "CREATE TABLE IF NOT EXISTS player_data ( SteamID TEXT, Money INTEGER )" ) end function NewPlayerToDataBase(ply) sql.Query("INSERT INTO player_data(SteamID, Money) VALUES('"..ply:SteamID().."', 0)") function SavePlayerToDataBase( ply, Money ) local data = sql.Query( "SELECT * FROM player_data WHERE SteamID = " .. sql.SQLStr( ply:SteamID() ) .. ";") if ( data ) then⤶ sql.Query( "UPDATE player_data SET Money = " .. Money .. " WHERE SteamID = " .. sql.SQLStr( ply:SteamID() ) .. ";" )⤶ else⤶ sql.Query( "INSERT INTO player_data ( SteamID, Money ) VALUES( " .. sql.SQLStr( ply:SteamID() ) .. ", " .. Money .. " )" )⤶ end⤶ end function SavePlayerToDatabase(ply, Money) sql.Query("UPDATE player_data SET Money="..Money.." WHERE SteamID='"..ply:SteamID().."'") function LoadPlayerToDataBase( ply ) local val = sql.QueryValue( "SELECT Money FROM player_data WHERE SteamID = " .. sql.SQLStr( ply:SteamID() ) .. ";" ) return val⤶ end </code> </example>