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

Is it any faster? Looks very nicely done
Reply
#3

Speed doesn't matter much. It should be just as fast as doing it all with db_query. Only difference is that this loops through all rows, all fields...

And thanks!
Reply
#4

Nice one
Reply
#5

Well, you could do this but with MySQL plugin BlueG
Reply
#6

Quote:
Originally Posted by DayvisonJJB
Посмотреть сообщение
Well, you could do this but with MySQL plugin BlueG
Thats why it has ORM.
Reply
#7

(351 line) db_get_int("pMoney", pInfo[playerid][pMoney]);

Quote:

C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(351) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(354) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(359) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(361) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(362) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(363) : error 017: undefined symbol "value"
C:\Users\Marius\Desktop\sa\GameMode\gamemodes\Okla homa.pwn(364) : error 017: undefined symbol "value"

Reply
#8

Seems good, although I'm sure I'll still use MySQL.
Reply
#9

Adding inline support would be great.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)