SQLite won't create database
#1

Hello.

I'm having problems, It seem like my SQLite doesn't save player stats.
I mean, I've added something (related to SQLite) and it stop creating database or saving player stats.

Can someone explain me why this happens?
Here are the codes, (Not the full codes)

pawn Код:
public OnGameModeInit()
{
    Database = db_open("userdata.db");
    db_query(Database,
    "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, ip VARCHAR(16) COLLATE NOCASE, password VARCHAR(129), hours INTEGER DEFAULT 0 NOT NULL, minutes INTEGER DEFAULT 0 NOT NULL, seconds INTEGER DEFAULT 0 NOT NULL, admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, speedboost INTEGER DEFAULT 1 NOT NULL)");
    return 1;
}

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

    new
        Query[82],
        DBResult:Result
    ;
    GetPlayerIp(playerid, iP[playerid], 16);
    format(Query, sizeof(Query), "SELECT 'password' FROM 'users' WHERE 'username' = '%s' LIMIT 0, 1", DB_Escape(GetName(playerid)));
    Result = db_query(Database, Query);
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "ip", User[playerid][IP], 16);
        db_get_field_assoc(Result, "password", User[playerid][Password], 129);
        if((!strcmp(iP[playerid], User[playerid][IP], true)))
        {
            LoginPlayer(playerid);
        }
        else
        {
            ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD,
            "xPS : Login", ""white"Welcome back to "#SERVER_NAME"\nYou cannot play the game if you don't login, Here's some several reasons:\n"red"• "white"Your stats will not save\n"red"• "white"To avoid ban evade!\n\n"red"NOTE: "white"If this is not your account, Please left the server and use another username!\n\nPlease input your account's password!", "Login", "");
        }
    }
    else ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD,
    "xPS : Register", ""white"Welcome to "#SERVER_NAME"\nHere are some several reason why you need to register:\n\n"red"• "white"Stats needed to saved in your account!\n"red"• "white"To avoid ban evading!\n\nPlease input the password you want to set to your account!", "Register", "Leave");
    db_free_result(Result);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    KillTimer(gametime[playerid]);
    if(User[playerid][LogIn] == true)
    {
        new
            Query[72]
        ;
        format(Query, sizeof(Query),
        "UPDATE users SET admin = %d, SET vip = %d, SET speedboost = %d, SET hours = %d, SET minutes = %d, SET seconds = %d WHERE username = '%s'",
        User[playerid][Admin],
        User[playerid][VIP],
        User[playerid][SBoost],
        User[playerid][Hours],
        User[playerid][Minutes],
        User[playerid][Seconds],
        DB_Escape(GetName(playerid)));
        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[])
{
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            if(response)
            {
                if(!IsValidPassword(inputtext))
                {
                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD,
                    "xPS : Register", ""white"Welcome to "#SERVER_NAME"\nHere are some several reason why you need to register:\n\n"red"• "white"Stats needed to saved in your account!\n"red"• "white"To avoid ban evading!\n\nPlease input the password you want to set to your account!\n\n"red"ERROR: "white"Your password contains invalid characters!", "Register", "Leave");
                    return 1;
                }
                if(strlen(inputtext) < 3 || strlen(inputtext) > 24)
                {
                    ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD,
                    "xPS : Register", ""white"Welcome to "#SERVER_NAME"\nHere are some several reason why you need to register:\n\n"red"• "white"Stats needed to saved in your account!\n"red"• "white"To avoid ban evading!\n\nPlease input the password you want to set to your account!\n\n"red"ERROR: "white"Your password length is short or it is very to long!", "Register", "Leave");
                    return 1;
                }
                new
                    Query[208]
                ;
                WP_Hash(User[playerid][Password], 129, inputtext);
                GetPlayerIp(playerid, iP[playerid], 16);
                format(Query, sizeof(Query),
                "INSERT INTO users (username, ip, password) VALUES ('%s', '%s', '%s')",
                    DB_Escape(GetName(playerid)),
                    iP[playerid],
                    DB_Escape(User[playerid][Password])
                );
                db_query(Database, Query);

                User[playerid][LogIn] = true;
                SendClientMessage(playerid, COLOR_YELLOW, "You have just registered to "#SERVER_NAME"! You have been automatically logged in!");
            }
            else Kick(playerid);
        }
        case DIALOG_LOGIN:
        {
            if(response)
            {
                new
                    buf[129]
                ;
                WP_Hash(buf, 129, inputtext);
                if(!strcmp(buf, User[playerid][Password], false))
                {
                    LoginPlayer(playerid);
                }
                else
                {
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD,
                    "xPS : Login", ""white"Welcome back to "#SERVER_NAME"\nYou cannot play the game if you don't login, Here's some several reasons:\n"red"• "white"Your stats will not save\n"red"• "white"To avoid ban evade!\n\n"red"NOTE: "white"If this is not your account, Please left the server and use another username!\n\nPlease input your account's password!\n"red"ERROR: "white"Incorrect Password!", "Login", "");
                }
            }
            else Kick(playerid);
        }
    }
    return 1;
}
Reply
#2

pawn Код:
format(Query, sizeof(Query), "SELECT 'password' FROM 'users' WHERE 'username' = '%s' LIMIT 0, 1", DB_Escape(GetName(playerid)));
    Result = db_query(Database, Query);
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "ip", User[playerid][IP], 16);
        db_get_field_assoc(Result, "password", User[playerid][Password], 129);
        if((!strcmp(iP[playerid], User[playerid][IP], true)))
        {
            LoginPlayer(playerid);
        }
        else
        {
            ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD,
            "xPS : Login", ""white"Welcome back to "#SERVER_NAME"\nYou cannot play the game if you don't login, Here's some several reasons:\n"red"• "white"Your stats will not save\n"red"• "white"To avoid ban evade!\n\n"red"NOTE: "white"If this is not your account, Please left the server and use another username!\n\nPlease input your account's password!", "Login", "");
        }
    }
Selecting only password and then trying to get data (ip and password fields). That's an incorrect way.

Also make sure that the query's lenght is enough in OnPlayerDisconnect.
Reply
#3

So i've to "SELECT * FROM users"
right?

I'd everything you said. But it still don't work.

Second, This is actually from your tutorial.
Reply
#4

Quote:
Originally Posted by _Jake_
Посмотреть сообщение
So i've to "SELECT * FROM users"
right?

I'd everything you said. But it still don't work.

Second, This is actually from your tutorial.
There are two ways:

- Either selecting only password and storing it to a variable and then compare the password the player typed with the one you've stored. If they do match, select all and assign them to the variables.
- Or selecting all and assign them to the variables (a bit better to be honest).

Then you should re-read it. I selected password and I stored the password - you selected password and you tried to store ip and password while ip is not even selected to the query. About the lenght, it's better to count - I always do!
Reply
#5

It saves now, I've discovered the problem, It was the query string size.
It won't create because the query string size is not enough to create all the tables that i want to created.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)