[Include] Easier SQLite loading
#1

Easier loading for SQLite

Just a simple include that uses y_ini style loading for SQLite data. Credits to ****** for the idea.

Usage
db_load does all of the magic:

Код:
/*
    Function:
        db_load
    Parameters:
        DB:database - The database handle.
        extraid - Used for passing an extra ID (example: playerid)
        function[] - Name of the function pass the data to.
        query[] - Query to execute.
        ... - Additional arguments.
    Returns:
        The number of rows returned from the query.
*/

native db_load(DB:database, extraid, const function[], const query[], {Float, DB, _}:...);
You must specify a callback to "function[]" which the data will be passed to. This is what it should like:

Код:
forward OnSQLiteLoad(extraid, row, const field[], const name[]);
Parameter names don't matter.

Let's break that down.

Код:
/*
    Callback:
        OnSQLiteLoad
    Parameters:
        extraid - Extra ID, passed earlier in db_load.
        row - Index of the row. Starts from zero!
        field[] - Name of the field.
        value[] - Self explanatory.
    Returns:
        No significant value.
*/
This is how loading player data should look like:

Код:
db_load(gDatabase, playerid, "OnLoadPlayerData", "SELECT * FROM users WHERE username = '%s'", name);
Код:
forward OnLoadPlayerData(playerid, row, const field[], const name[]);
public OnLoadPlayerData(playerid, row, const field[], const name[])
{
    db_get_int("money", gPlayerMoney[playerid]);
    db_get_float("health", gPlayerHealth[playerid]);
    db_get_string("accent", gPlayerAccent[playerid], 24);
}
Multiple rows
You can also load multiple rows at a time!

Код:
new
    rows = db_load(gDatabase, -1, "OnLoadHouses", "SELECT * FROM houses");

printf("%d houses loaded!", rows);
Код:
forward OnLoadHouses(extraid, row, const field[], const name[]);
public OnLoadHouses(extraid, row, const field[], const name[])
{
    /* 
     *  extraid is not needed here.
     *  Instead, we will use "row", which contains the index of the row.
    */

    db_get_int("id", gHouseData[row][hID]);
    db_get_string("owner", gHouseData[row][hOwner], MAX_PLAYER_NAME);
    db_get_int("price", gHouseData[row][hPrice]);
    db_get_float("x", gHouseData[row][hX]);
    db_get_float("y", gHouseData[row][hY]);
    db_get_float("z", gHouseData[row][hZ]);
}
What's the point?
- Better organization of code.
- Easy to use. No need to store the result, loop through the rows, free the result...
- Load as much data as you want.

Download
http://pastebin.com/KPkJ7eza
Reply


Messages In This Thread
Easier SQLite loading - by Emmet_ - 03.04.2015, 09:32
Re: Easier SQLite loading - by Sellize - 03.04.2015, 09:41
Re: Easier SQLite loading - by Emmet_ - 03.04.2015, 09:43
Re : Easier SQLite loading - by AlexBlack - 03.04.2015, 21:13
Re: Easier SQLite loading - by Dayvison_ - 03.04.2015, 21:26
Re: Easier SQLite loading - by arakuta - 03.04.2015, 21:42
Re: Easier SQLite loading - by Depp - 07.07.2015, 10:39
Re: Easier SQLite loading - by JonathanW - 07.07.2015, 11:02
Re: Easier SQLite loading - by Patrick - 04.08.2015, 16:28

Forum Jump:


Users browsing this thread: 1 Guest(s)