[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
#2

Another useful include, damn Emmet_. Good work.
Reply
#3

You are a great scripter, good job
Reply
#4

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

Thanks guys.

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

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

another cool add on for SQLite good work!
Reply
#8

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?
Reply
#9

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!
Reply
#10

with this it's more easy to use

thanks and great job

@ OFF

with sqlite is possible put Default values in table?
Reply
#11

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
Reply
#12

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?
Reply
#13

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.
Reply
#14

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.
Reply
#15

Good job , very nice and helpfull , +rep.
Reply
#16

Thanks for this it will tidy up my code a LOT
Reply
#17

Updated!
  • You can now use unformatted whence statements!
  • The code has been updated, and now uses less memory than before.
  • Fixed a bug with the wrong query size being used.
  • Added 3 functions: db_fetch_bool, db_next_parse and db_get_result.
Reply
#18

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

Very Good Job emmet! You are impressive!
Reply
#20

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.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)