Database Queries
cLib.MYSQL.f.Query
Execute a SQL query with prepared statements.
cLib.MYSQL.f.Query(db, sql, params, onSuccess, onError)Parameters:
| Parameter | Type | Description |
|---|---|---|
db | Database | Database connection |
sql | string | SQL query with ? placeholders |
params | table | Parameter values (in order) |
onSuccess | function | Success callback(results) |
onError | function | Error 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
)