Database not saving (sqlite)
#1

Hey guys,

So I'm working on a new project but my sqlite database is not saving my stats, it does save the password so... I have no idea what is wrong, I get no compiling errors.

pawn Код:
#define MAX_INI_ENTRY_TEXT 80

native WP_Hash(buffer[], len, const str[]);

enum USER_DATA
{
    USER_ID,
    USER_NAME[MAX_PLAYER_NAME],
    USER_PASSWORD[129],
    USER_ADMIN,
    USER_VIP,
    USER_SCORE,
    USER_KILLS,
    USER_DEATHS,

    bool: USER_LOGGED_IN
};

new User[MAX_PLAYERS][USER_DATA];
new DB: Database;

Database = db_open("server.db");
    db_query(Database, "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, password VARCHAR(129), admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, score INTEGER DEFAULT 0 NOT NULL, kills INTEGER DEFAULT 0 NOT NULL, deaths INTEGER DEFAULT 0 NOT NULL)");

public OnPlayerConnect(playerid)
{
    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;

    GetPlayerName(playerid, User[playerid][USER_NAME], MAX_PLAYER_NAME);

    new Query[71], DBResult: Result;
    format(Query, sizeof(Query), "SELECT password FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
    Result = db_query(Database, Query);
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "password", User[playerid][USER_PASSWORD], 129);
        ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_RED"Unlimited-Gaming"COL_WHITE" Login pannel", ""COL_WHITE"Please login to play on "COL_RED"DayZ Evolution\n"COL_WHITE"Enter your "COL_RED"password"COL_WHITE":", "Login", "Exit");
    }
    else ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, ""COL_RED"Unlimited-Gaming"COL_WHITE" Register pannel", ""COL_WHITE"Please register to play on "COL_RED"DayZ Evolution\n"COL_WHITE"Enter a "COL_RED"password"COL_WHITE":", "Register", "Exit");
    db_free_result(Result);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(User[playerid][USER_LOGGED_IN] == true)
    {
        new Query[128];
        format(Query, sizeof(Query), "UPDATE users SET admin = %i, vip = %d, score = %d, kills = %d, deaths = %d, WHERE username = '%s'", User[playerid][USER_ADMIN], User[playerid][USER_VIP], GetPlayerScore(playerid), User[playerid][USER_KILLS], User[playerid][USER_DEATHS], DB_Escape(User[playerid][USER_NAME]));
        db_query(Database, Query);
    }
    for(new i; i < _: USER_DATA; ++i) User[playerid][USER_DATA: i] = 0;
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 0)
    {
        if(response)
        {
            if(!inputtext[0]) return ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_RED"Unlimited-Gaming"COL_WHITE" Login pannel", ""COL_WHITE"Please login to play on "COL_RED"DayZ Evolution\n "COL_WHITE"Enter your "COL_RED"password"COL_WHITE":", "Login", "Exit");
            new buf[129];
            WP_Hash(buf, 129, inputtext);
            if(!strcmp(buf, User[playerid][USER_PASSWORD], false))
            {
                new Query[75], DBResult: Result;
                format(Query, sizeof(Query), "SELECT * FROM users WHERE username = '%s' LIMIT 0, 1", DB_Escape(User[playerid][USER_NAME]));
                Result = db_query(Database, Query);
                if(db_num_rows(Result))
                {
                    db_get_field_assoc(Result, "userid", Query, 10);
                    User[playerid][USER_ID] = strval(Query);

                    db_get_field_assoc(Result, "admin", Query, 10);
                    User[playerid][USER_ADMIN] = strval(Query);

                    db_get_field_assoc(Result, "vip", Query, 10);
                    User[playerid][USER_VIP] = strval(Query);

                    db_get_field_assoc(Result, "score", Query, 10);
                    User[playerid][USER_SCORE] = strval(Query);

                    db_get_field_assoc(Result, "kills", Query, 10);
                    User[playerid][USER_KILLS] = strval(Query);

                    db_get_field_assoc(Result, "deaths", Query, 10);
                    User[playerid][USER_DEATHS] = strval(Query);

                    User[playerid][USER_LOGGED_IN] = true;

                    SetPlayerScore(playerid, User[playerid][USER_SCORE]);

                    SendClientMessage(playerid, -1, ""chat" You have successfully logged in to your account!");
                }
                db_free_result(Result);
            }
            else
            {
                SendClientMessage(playerid, -1, ""chat" "COL_RED"Incorrect password!");
                ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, ""COL_RED"Unlimited-Gaming"COL_WHITE" Login pannel", ""COL_WHITE"Please login to play on "COL_RED"DayZ Evolution\n"COL_WHITE"Enter your "COL_RED"password"COL_WHITE":", "Login", "Exit");

            }
        }
        else Kick(playerid);
        return 1;
    }

stock DB_Escape(text[])
{
    new ret[ 80 * 2 ], ch, i, j;
    while((ch = text[i++]) && j < sizeof(ret))
    {
        if(ch == '\'')
        {
            if(j < sizeof(ret) - 2)
            {
                ret[j++] = '\'';
                ret[j++] = '\'';
            }
        }
        else if(j < sizeof(ret))
        {
            ret[j++] = ch;
        }
        else
        {
            j++;
        }
    }
    ret[sizeof(ret) - 1] = '\0';
    return ret;
}
It simply doesnt save the stats like admin, vip, score, kills and deaths.
You guys notice anything off?

Thanks
Reply


Messages In This Thread
Database not saving (sqlite) - by Voxel - 15.03.2015, 14:55

Forum Jump:


Users browsing this thread: 1 Guest(s)