SA-MP Forums Archive
[Include] db_parse.inc - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] db_parse.inc (/showthread.php?tid=477550)



db_parse.inc - Emmet_ - 24.11.2013

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


Re: db_parse.inc - iZN - 24.11.2013

Another useful include, damn Emmet_. Good work.


AW: db_parse.inc - Mellnik - 24.11.2013

You are a great scripter, good job


Re: db_parse.inc - carloLT - 24.11.2013

nice work, but i don't like sqlite :/


Re: db_parse.inc - Emmet_ - 24.11.2013

Thanks guys.

Well, if anyone does decide to use it, feel free to suggest anything or report any bugs!


Re: db_parse.inc - Slice - 28.11.2013

Quote:
Originally Posted by carloLT
View Post
nice work, but i don't like sqlite :/
Why not?


Re: db_parse.inc - Niko_boy - 28.11.2013

another cool add on for SQLite good work!


Re: db_parse.inc - theYiin - 28.11.2013

Why did we need DB:db variable, if it is unused? And what if I want to open more than one db file at once?


Re: db_parse.inc - Konstantinos - 28.11.2013

Very nice, Emmet!

Quote:
Originally Posted by theYiin
View Post
Why did we need DB:db variable, if it is unused? And what if I want to open more than one db file at once?
It's not unused, to execute a query you'll need to use the ID of the database; hence it's stored to db.

I suppose you can open more databases, but you can make sure by doing a test!


Re: db_parse.inc - PT - 28.11.2013

with this it's more easy to use

thanks and great job

@ OFF

with sqlite is possible put Default values in table?


Re: db_parse.inc - Konstantinos - 28.11.2013

Quote:
Originally Posted by PT
View Post
with sqlite is possible put Default values in table?
Yes, it is.

pawn Code:
field_name_here data_type_here DEFAULT default_value_here NOT NULL
Examples:
pawn Code:
some_integer INTEGER DEFAULT 0 NOT NULL
some_string VARCHAR(24) DEFAULT '-' NOT NULL
some_float FLOAT DEFAULT 0.0 NOT NULL



Re: db_parse.inc - PT - 28.11.2013

Thanks Konstantinos reputed!

but more one question..

in mysql for exemple in phpmyadmin you can put ther the values...

for SQLite in using the "SQLite Database Browser 2.0 b1", and i dont find their were put the lenght of a row for exemple or default value..

can you explain for me please?


Re: db_parse.inc - Emmet_ - 29.11.2013

Quote:
Originally Posted by carloLT
View Post
nice work, but i don't like sqlite :/
IMO, SQLite is one of the the best options for remotely saving data, without having to connect an external database or having multiple INI files floating about. If you know MySQL then you can use SQLite to the fullest extent.

However, if you don't know a lot of SQL then you can use one of the great libraries released around the forums that are designed to make it easier for you!

Quote:
Originally Posted by PT
View Post
Thanks Konstantinos reputed!

but more one question..

in mysql for exemple in phpmyadmin you can put ther the values...

for SQLite in using the "SQLite Database Browser 2.0 b1", and i dont find their were put the lenght of a row for exemple or default value..

can you explain for me please?
Uhm, yeah.

First, you load up the browser and select your DB, then select "Execute SQL". You can execute those queries that Konstantinos explained in his post in that box.


Re: db_parse.inc - Ballu Miaa - 29.11.2013

This looks great. I love SQLite and i would love to use this too. It will optimize all of my future scripts in SQLite. Very nice Emmet. Will report bugs if any after testing it.


Re: db_parse.inc - DowDaw - 07.12.2013

Good job , very nice and helpfull , +rep.


Re: db_parse.inc - Jay_ - 07.12.2013

Thanks for this it will tidy up my code a LOT


Re: db_parse.inc - Emmet_ - 26.12.2013

Updated!



Re: db_parse.inc - Cypress - 26.12.2013

That's a really nice include. Though, I am already using slice's include, code is organized as well.


Re: db_parse.inc - ProjectNewYork - 26.12.2013

Very Good Job emmet! You are impressive!


Re: db_parse.inc - Emmet_ - 26.12.2013

Quote:
Originally Posted by ProjectNewYork
View Post
Very Good Job emmet! You are impressive!
Thanks, man!

Quote:
Originally Posted by Cypress
View Post
That's a really nice include. Though, I am already using slice's include, code is organized as well.
Thanks! Slice's include is great and I'm also using it atm.