Garry's Mod Wiki

Revision Difference

sql.QueryValue#529366

<function name="QueryValue" parent="sql" type="libraryfunc"> <description>Performs the query like <page>sql.QueryRow</page>, but returns the first value found.</description> <realm>Shared and Menu</realm> <file line="68-L85">lua/includes/util/sql.lua</file> <args> <arg name="query" type="string">The input query.</arg> </args> <rets> <ret name="" type="any">The returned value; the engine automatically converts numerical output to Lua numbers.</ret> <ret name="" type="string">The returned value.</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 IF NOT EXISTS player_data ( SteamID TEXT, Money INTEGER )" )⤶ end⤶ ⤶ 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 LoadPlayerToDataBase( ply )⤶ local val = sql.QueryValue( "SELECT Money FROM player_data WHERE SteamID = " .. sql.SQLStr( ply:SteamID() ) .. ";" )⤶ return val⤶ end⤶ </code>⤶ ⤶ </example>