[Include] db_parse.inc
#1

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:

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);
Looks very messy, but with db_parse:

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();
Looks VERY neatly organized now, doesn't it?

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();
Download
Pastebin
Solidfiles
Reply


Messages In This Thread
db_parse.inc - by Emmet_ - 24.11.2013, 17:38
Re: db_parse.inc - by iZN - 24.11.2013, 17:47
AW: db_parse.inc - by Mellnik - 24.11.2013, 17:57
Re: db_parse.inc - by carloLT - 24.11.2013, 19:23
Re: db_parse.inc - by Emmet_ - 24.11.2013, 19:27
Re: db_parse.inc - by Slice - 28.11.2013, 07:42
Re: db_parse.inc - by Niko_boy - 28.11.2013, 09:17
Re: db_parse.inc - by theYiin - 28.11.2013, 10:55
Re: db_parse.inc - by Konstantinos - 28.11.2013, 11:08
Re: db_parse.inc - by PT - 28.11.2013, 18:57
Re: db_parse.inc - by Konstantinos - 28.11.2013, 20:04
Re: db_parse.inc - by PT - 28.11.2013, 20:49
Re: db_parse.inc - by Emmet_ - 29.11.2013, 00:24
Re: db_parse.inc - by Ballu Miaa - 29.11.2013, 02:36
Re: db_parse.inc - by MagicSamp - 07.12.2013, 13:47
Re: db_parse.inc - by Jay_ - 07.12.2013, 13:50
Re: db_parse.inc - by Emmet_ - 26.12.2013, 07:20
Re: db_parse.inc - by Cypress - 26.12.2013, 11:56
Re: db_parse.inc - by ProjectNewYork - 26.12.2013, 12:06
Re: db_parse.inc - by Emmet_ - 26.12.2013, 13:01

Forum Jump:


Users browsing this thread: 4 Guest(s)