open.mp forum
[Pawn] Health not saving in SQLite? - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Health not saving in SQLite? (/showthread.php?tid=2284)



Health not saving in SQLite? - CrypticSin - 2022-04-14

Trying to save player health but doesn't seem to be working i'll like to know what im doing wrong?

Created a table in OnGameModeInit:
PHP Code:
db_query(server_database"CREATE TABLE IF NOT EXISTS `USERS` (`NAME`, `IP`, `REGISTER_DATE`, `PASS`, `SCORE`, `KILLS`, `DEATHS`, `SKIN`, `HEALTH`, `pX`, `pY`, `pZ`, `pA`, `INT`, `WORLD`, `ADMIN`)");; 

Selecting data:
PHP Code:
public OnPlayerLogin(playeridbool:success)
{
    if(
success)
    {
        new 
query[256], field[24], field2[64];
        
format(querysizeof(query), "SELECT * FROM `USERS` WHERE `NAME` = '%s' COLLATE NOCASE"DB_Escape(ReturnName(playerid)));
        
database_result db_query(server_databasequery);
        if(
db_num_rows(database_result))
        {
            
db_get_field_assoc(database_result"ADMIN"fieldsizeof(field));
            
PlayerInfo[playerid][pAdmin] = strval(field);

            
db_get_field_assoc(database_result"SCORE"fieldsizeof(field));
            
SetPlayerScore(playeridstrval(field));

            
db_get_field_assoc(database_result"KILLS"fieldsizeof(field));
            
PlayerInfo[playerid][pKills] = strval(field);

            
db_get_field_assoc(database_result"DEATHS"fieldsizeof(field));
            
PlayerInfo[playerid][pDeaths] = strval(field);

            
db_get_field_assoc(database_result"SKIN"fieldsizeof(field));
            
PlayerInfo[playerid][pSkin] = strval(field);

            
db_get_field_assoc(database_result"REGISTER_DATE"field2sizeof(field2));
            
PlayerInfo[playerid][pRegister] = field2;

            
db_get_field_assoc(database_result"HEALTH"fieldsizeof(field));
            
PlayerInfo[playerid][pHealth] = floatstr(field);

            
db_get_field_assoc(database_result"pX"fieldsizeof(field));
            
PlayerInfo[playerid][pX] = floatstr(field);

            
db_get_field_assoc(database_result"pY"fieldsizeof(field));
            
PlayerInfo[playerid][pY] = floatstr(field);

            
db_get_field_assoc(database_result"pZ"fieldsizeof(field));
            
PlayerInfo[playerid][pZ] = floatstr(field);

            
db_get_field_assoc(database_result"pA"fieldsizeof(field));
            
PlayerInfo[playerid][pA] = floatstr(field);

            
db_get_field_assoc(database_result"INT"fieldsizeof(field));
            
PlayerInfo[playerid][pInt] = strval(field);

            
db_get_field_assoc(database_result"WORLD"fieldsizeof(field));
            
PlayerInfo[playerid][pWorld] = strval(field);
        } 

Inserting Health Data:
PHP Code:
? ??
    new 
query[400];
    
format(querysizeof(query), "INSERT INTO `USERS` (`NAME`, `PASS`, `ADMIN`, `IP`, `REGISTER_DATE`, `SCORE`, `KILLS`, `DEATHS`, `SKIN`, `HEALTH`, `pX`, `pY`, `pZ`, `pA`, `INT`, `WORLD`) VALUES ('%s', '%s', %d, '%s', '%s', %d, %d, %d, %d, %f, %f, %f, %f, %f, %d, %d)"DB_Escape(ReturnName(playerid)), passwordPLAYERipDateGetPlayerScore(playerid), PlayerInfo[playerid][pKills], PlayerInfo[playerid][pDeaths], GetPlayerSkin(playerid), PlayerInfo[playerid][pHealth],
    
PlayerInfo[playerid][pX],
    
PlayerInfo[playerid][pY],
    
PlayerInfo[playerid][pZ],
    
PlayerInfo[playerid][pA],
    
PlayerInfo[playerid][pInt],
    
PlayerInfo[playerid][pWorld]);
    
database_result db_query(server_databasequery);
    
db_free_result(database_result); 

Updating Data:

PHP Code:
? ? ? ? ? ??
stock SaveAccount(playerid)
{
    new 
query[400], Float:pos[4];
    if(
PlayerInfo[playerid][pLoggedIn] == true)
    {
        
GetPlayerPos(playeridpos[0], pos[1], pos[2]);
        
GetPlayerFacingAngle(playeridpos[3]);
        
GetPlayerHealth(playeridPlayerInfo[playerid][pHealth]);

        
format(querysizeof(query),
        
"UPDATE `USERS` SET `ADMIN` = %d, `SCORE` = %d, `KILLS` = %d, `DEATHS` = %d, `SKIN` = %d, `HEALTH` = %f, `pX` = %f, `pY` = %f, `pZ` = %f, `pA` = %f, `INT` = %d, `WORLD` = %d, `ADMIN` = %d WHERE `NAME` = '%s' COLLATE NOCASE",
        
PlayerInfo[playerid][pAdmin], GetPlayerScore(playerid), PlayerInfo[playerid][pKills], PlayerInfo[playerid][pDeaths], GetPlayerSkin(playerid), PlayerInfo[playerid][pHealth], pos[0], pos[1], pos[2], pos[3], GetPlayerInterior(playerid), GetPlayerVirtualWorld(playerid), PlayerInfo[playerid][pAdmin], DB_Escape(ReturnName(playerid)));
        
database_result db_query(server_databasequery);
        
db_free_result(database_result);
    }
    return 
1;


Code runs without errors but im still unsure on why its not saving and loading the players health..


RE: Health not saving in SQLite? - Banditul - 2022-04-14

I'm suprised your create table query even runs because is so wrong

https://www.sqlitetutorial.net/sqlite-create-table/


RE: Health not saving in SQLite? - CrypticSin - 2022-04-14

(2022-04-14, 07:25 AM)Banditul Wrote: I'm suprised your create table query even runs because is so wrong

https://www.sqlitetutorial.net/sqlite-create-table/



Everything is working fine, my next step is to have the health system saved.?



I know it's not good practice but, ill fix all those later once i fix some of the features i want to add on the server.


RE: Health not saving in SQLite? - CrypticSin - 2022-04-18

I've updated the code slightly and it does run but the Health data still doesn't show on sqlite browser. Any ideas?