How could I...? -
Scenario - 12.12.2011
I am looking for an efficient way to save a player's weapon data (for all 13 slots) into a MySQL database. Is it possible by doing an array, that doesn't query the server 13 times?
Re: How could I...? -
[HiC]TheKiller - 13.12.2011
You could make one big query or you could make it a string. Making it a string was a method I used a while ago to save all vehicle mods, weapons etc in one line in the database. Later, I would split it with sscanf into separate categories.
pawn Код:
new weapondata[2], savestr[250], pname[24];
GetPlayerName(playerid, pname, 24);
for(new x; x<13; x++)
{
GetPlayerWeaponData(playerid, x, weapondata[0], weapondata[1]);
format(savestr, sizeof(savestr), "%s%d||%d||", savestr, weapondata[0], weapondata[1]);
}
new query[320];
format(query, sizeof(query), "UPDATE users SET weapondata = '%s' WHERE username = '%s', savestr, pname);
mysql_query(query);
That would just save as a bunch of numbers that you could split with sscanf later on. I'm not sure if it's really efficient but it works well.
pawn Код:
new wd[2][13], pname[24], query[150], wdataline[250];
GetPlayerName(playerid, pname, 24);
format(query, sizeof(query), "SELECT weapondata from users WHERE username = '%s'", pname);
mysql_query(query);
mysql_store_result();
mysql_fetch_field_row(wdataline, "weapondata");
mysql_free_result();
sscanf(wdataline, "p<||>ddddddddddddddddddddddddddd", wd[0][0], wd[1][0], wd[0][1], wd[1][1], wd[0][2], wd[1][2], wd[0][3], wd[1][3], wd[0][4], wd[1][4], wd[0][5], wd[1][5], wd[0][6], wd[1][6], wd[0][7], wd[1][7], wd[0][8], wd[1][8], wd[0][9], wd[1][9], wd[0][10], wd[1][10], wd[0][11], wd[1][11], wd[0][12], wd[1][12]);
for(new x; x<13; x++)
{
if(wd[x][0] != 0) GivePlayerWeapon(playerid, wd[x][0], wd[x][1]);
}
You kinda get the idea there. Anyway, you can also just have a bunch of variables in an update query

.
//You can probably do this some other way, but I'm not that great with sscanf
Re: How could I...? -
Scenario - 13.12.2011
Quote:
Originally Posted by [HiC]TheKiller
You could make one big query or you could make it a string. Making it a string was a method I used a while ago to save all vehicle mods, weapons etc in one line in the database. Later, I would split it with sscanf into separate categories.
pawn Код:
new weapondata[2], savestr[250], pname[24]; GetPlayerName(playerid, pname, 24); for(new x; x<13; x++) { GetPlayerWeaponData(playerid, x, weapondata[0], weapondata[1]); format(savestr, sizeof(savestr), "%s%d||%d||", savestr, weapondata[0], weapondata[1]); } new query[320]; format(query, sizeof(query), "UPDATE users SET weapondata = '%s' WHERE username = '%s', savestr, pname); mysql_query(query);
That would just save as a bunch of numbers that you could split with sscanf later on. I'm not sure if it's really efficient but it works well.
pawn Код:
new wd[2][13], pname[24], query[150], wdataline[250]; GetPlayerName(playerid, pname, 24); format(query, sizeof(query), "SELECT weapondata from users WHERE username = '%s'", pname); mysql_query(query); mysql_store_result(); mysql_fetch_field_row(wdataline, "weapondata"); mysql_free_result(); sscanf(wdataline, "p<||>ddddddddddddddddddddddddddd", wd[0][0], wd[1][0], wd[0][1], wd[1][1], wd[0][2], wd[1][2], wd[0][3], wd[1][3], wd[0][4], wd[1][4], wd[0][5], wd[1][5], wd[0][6], wd[1][6], wd[0][7], wd[1][7], wd[0][8], wd[1][8], wd[0][9], wd[1][9], wd[0][10], wd[1][10], wd[0][11], wd[1][11], wd[0][12], wd[1][12]); for(new x; x<13; x++) { if(wd[x][0] != 0) GivePlayerWeapon(playerid, wd[x][0], wd[x][1]); }
You kinda get the idea there. Anyway, you can also just have a bunch of variables in an update query  .
//You can probably do this some other way, but I'm not that great with sscanf
|
I like your idea. I ended up just saving 26 fields in the DB for weapons/ammo, and then having 13 lines to retrieve and give the player their weapon/ammo. I might run some speed tests here in a few, otherwise, this will be just fine for me. By the way, sscanf and I don't get a long very well either.

Thanks again!