SQLite help.
#1

Hi, mi problem is that when i want to get an user variables with sqlite the system don't get the correct values, for example, when i log in with a account with field "admin" = 8, db_get_field_assoc_int is always getting value 0, instead of 8.

Here's my code:
Код:
LoginPlayer(playerid)
{
new Query[100], DBResult: Result2;
format(Query, sizeof(Query), "SELECT username FROM users WHERE username = '%s' LIMIT 0, 1", Nick(playerid));
                Result2 = db_query(Database, Query);
                if(db_num_rows(Result2))
                {
                    PlayerInfo[playerid][IID] = db_get_field_assoc_int(Result2, "userid");
                    PlayerInfo[playerid][Level] = db_get_field_assoc_int(Result2, "admin");
                    PlayerInfo[playerid][pVip] = db_get_field_assoc_int(Result2, "vip");
                    PlayerInfo[playerid][Money] = db_get_field_assoc_int(Result2, "money");
                    PlayerInfo[playerid][Score] = db_get_field_assoc_int(Result2, "score");
                    PlayerInfo[playerid][Kills] = db_get_field_assoc_int(Result2, "kills");
                    PlayerInfo[playerid][Deaths] = db_get_field_assoc_int(Result2, "deaths");
                    PlayerInfo[playerid][DGanados] = db_get_field_assoc_int(Result2, "dwin");
                    PlayerInfo[playerid][DPerdidos] = db_get_field_assoc_int(Result2, "dlost");
                    PlayerInfo[playerid][Experiencia] = db_get_field_assoc_int(Result2, "exp");
                    PlayerInfo[playerid][hourss] = db_get_field_assoc_int(Result2, "hours");
                    PlayerInfo[playerid][mins] = db_get_field_assoc_int(Result2, "mins");
                    PlayerInfo[playerid][secs] = db_get_field_assoc_int(Result2, "secs");
                    PlayerH[playerid][Spawn] = db_get_field_assoc_int(Result2, "pSpawn");
    		    PlayerH[playerid][Houseid] = db_get_field_assoc_int(Result2, "HouseID");
    				
		    PlayerInfo[playerid][Registered] = 1;
 		    PlayerInfo[playerid][LoggedIn] = 1;

                    SetPlayerMoney(playerid, PlayerInfo[playerid][Money]);
                    SetPlayerScore(playerid, PlayerInfo[playerid][Score]);
                }
                db_free_result(Result2);
}
Thanks.
Reply
#2

You are only selecting one field i.e. "username" in the query. So expect only one field's result.

And your query is pretty much all messed up.
- No use of "`" for table and column names
- LIMIT having multiple values

Corrected query:
pawn Код:
SELECT * FROM `users` WHERE `username` = '%s' LIMIT 1
Reply
#3

Quote:
Originally Posted by Gammix
Посмотреть сообщение
You are only selecting one field i.e. "username" in the query. So expect only one field's result.

And your query is pretty much all messed up.
- No use of "`" for table and column names
- LIMIT having multiple values

Corrected query:
pawn Код:
SELECT * FROM `users` WHERE `username` = '%s' LIMIT 1
Now working, thanks you a lot!! But one question, if im expecting only one field, then why i have to put "LIMIT 1"?
Reply
#4

Quote:
Originally Posted by LoLeRo
Посмотреть сообщение
Now working, thanks you a lot!! But one question, if im expecting only one field, then why i have to put "LIMIT 1"?
The LIMIT is for rows not fields, and you are not expecting one field, you are extracting data from more than 10 fields ("userid", "admin", "vip" etc.).

LIMIT 1 means only one row will be loaded in cache/result.
Reply
#5

Ok, but i need in this case to put "LIMIT 1" necessarily? Because as you said, i'm only expecting 1 row because there is only 1 username with that nick at all the table. I'm asking this because i have other querys in which i don't use LIMIT 1.
Thanks.
Reply
#6

Quote:
Originally Posted by LoLeRo
Посмотреть сообщение
Ok, but i need in this case to put "LIMIT 1" necessarily? Because as you said, i'm only expecting 1 row because there is only 1 username with that nick at all the table. I'm asking this because i have other querys in which i don't use LIMIT 1.
Thanks.
Well putting a limit can be called more efficient and faster than not because it basically limits the amount of data to be loaded. In case you are only using one row, use LIMIT 1.

Or all in all, use LIMIT where ever you know how much maximum it can go or how much maximum amount of rows you want.
Reply
#7

Ok thank you very much!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)