03.04.2015, 09:32
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:
You must specify a callback to "function[]" which the data will be passed to. This is what it should like:
Parameter names don't matter.
Let's break that down.
This is how loading player data should look like:
Multiple rows
You can also load multiple rows at a time!
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
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, _}:...);
Код:
forward OnSQLiteLoad(extraid, row, const field[], const name[]);
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.
*/
Код:
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);
}
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]);
}
- 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


