24.11.2013, 17:38
(
Last edited by Emmet_; 25/01/2014 at 02:18 PM.
)
db_parse
Introduction
Yesterday, I began development of a new system that uses SQLite, but I was going to run queries all around the clock, so formatting strings all the time wasn't too pretty looking, so I implemented this useful library that does it for you!
Tutorial: https://sampforum.blast.hk/showthread.php?tid=483457
What's the catch?
This code:
Looks very messy, but with db_parse:
Looks VERY neatly organized now, doesn't it?
Functions
There are 8 functions:
Download
Pastebin
Solidfiles
Introduction
Yesterday, I began development of a new system that uses SQLite, but I was going to run queries all around the clock, so formatting strings all the time wasn't too pretty looking, so I implemented this useful library that does it for you!
Tutorial: https://sampforum.blast.hk/showthread.php?tid=483457
What's the catch?
This code:
pawn Code:
new
string[128],
DBResult:result,
DB:db = db_open("Accounts.db");
format(string, sizeof(string), "SELECT * FROM `Accounts` WHERE `Username` = '%s'", PlayerName(playerid));
result = db_query(db, string);
if (db_num_rows(result) > 0)
{
db_get_field_assoc(result, "Money", string, sizeof(string));
PlayerInfo[playerid][pMoney] = strval(string);
db_get_field_assoc(result, "Score", string, sizeof(string));
PlayerInfo[playerid][pScore] = strval(string);
}
db_free_result(result);
pawn Code:
new
rows,
DB:db = db_open("Accounts.db");
rows = db_parse(db, "Accounts", "`Username` = '%s'", PlayerName(playerid));
if (rows)
{
PlayerInfo[playerid][pMoney] = db_fetch_int("Money");
PlayerInfo[playerid][pScore] = db_fetch_int("Score");
}
db_free_parse();
Functions
There are 8 functions:
pawn Code:
// Parse a database query for fetching results.
stock db_parse(DB:database, db_table[], string[] = "", ...);
// Returns the integer of the specified field name.
stock db_fetch_int(field[]);
// Returns the float value of the specified field name.
stock Float:db_fetch_float(field[]);
// Returns a boolean value for the specified field name.
stock bool:db_fetch_bool(field[]);
// Fetches the string from the specified field name.
stock db_fetch_string(field[], dest[], size = sizeof(dest));
// Get the stored result of the last executed query.
stock db_get_result(&DBResult:result);
// Free the stored result.
stock db_free_parse();
// Similar to "db_next_row" but for the stored result.
stock db_next_parse();
Pastebin
Solidfiles