Database Queries

Database Updated: Dec 10, 2025

Database Queries

cLib.MYSQL.f.Query

Execute a SQL query with prepared statements.

cLib.MYSQL.f.Query(db, sql, params, onSuccess, onError)

Parameters:

ParameterTypeDescription
dbDatabaseDatabase connection
sqlstringSQL query with ? placeholders
paramstableParameter values (in order)
onSuccessfunctionSuccess callback(results)
onErrorfunctionError callback(error)

SELECT Queries

-- Get single row
cLib.MYSQL.f.Query(db, 
    "SELECT * FROM players WHERE steamid = ?",
    {ply:SteamID64()},
    function(results)
        if results and results[1] then
            local playerData = results[1]
            print("Money:", playerData.money)
        end
    end
)

-- Get multiple rows
cLib.MYSQL.f.Query(db,
    "SELECT * FROM items WHERE category = ? ORDER BY name",
    {"weapons"},
    function(results)
        for _, item in ipairs(results or {}) do
            print(item.name, item.price)
        end
    end
)

-- With multiple conditions
cLib.MYSQL.f.Query(db,
    "SELECT * FROM logs WHERE steamid = ? AND action = ? LIMIT ?",
    {steamid, "purchase", 10},
    function(results)
        -- Process results
    end
)

INSERT Queries

-- Insert new row
cLib.MYSQL.f.Query(db,
    "INSERT INTO players (steamid, name, money) VALUES (?, ?, ?)",
    {ply:SteamID64(), ply:Nick(), 1000},
    function()
        print("Player created!")
    end
)

-- Insert or update (upsert)
cLib.MYSQL.f.Query(db,
    [[INSERT INTO players (steamid, name, money) VALUES (?, ?, ?)
      ON DUPLICATE KEY UPDATE name = VALUES(name)]],
    {ply:SteamID64(), ply:Nick(), 1000}
)

UPDATE Queries

-- Update single column
cLib.MYSQL.f.Query(db,
    "UPDATE players SET money = ? WHERE steamid = ?",
    {newMoney, ply:SteamID64()}
)

-- Update multiple columns
cLib.MYSQL.f.Query(db,
    "UPDATE players SET money = ?, playtime = playtime + ? WHERE steamid = ?",
    {newMoney, sessionTime, ply:SteamID64()}
)

-- Increment value
cLib.MYSQL.f.Query(db,
    "UPDATE players SET money = money + ? WHERE steamid = ?",
    {amount, ply:SteamID64()}
)

DELETE Queries

-- Delete row
cLib.MYSQL.f.Query(db,
    "DELETE FROM items WHERE id = ? AND owner = ?",
    {itemID, ply:SteamID64()}
)

-- Delete with condition
cLib.MYSQL.f.Query(db,
    "DELETE FROM sessions WHERE last_seen < DATE_SUB(NOW(), INTERVAL 30 DAY)"
)

Error Handling

cLib.MYSQL.f.Query(db, query, params,
    function(results)
        -- Success
    end,
    function(err)
        cLib.Debug.Error("Database error:", err)
        -- Handle error (notify admin, retry, etc.)
    end
)