SA-MP Forums Archive
Problem with SQL query - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Problem with SQL query (/showthread.php?tid=664995)



Problem with SQL query - Dolke - 18.03.2019

Hello.

I tried to make table that stores "Name" and "Level" of an Admin.

I just tried to update existing information in table with new but I cant get it to work.

Here is the code,I do not see what is wrong here (Maybe Im blind )

Код:
YCMD:makeadminslot(playerid, params[], help) 
{
	new id,level,slot,query[512],DB:base;
	if(PlayerInfo[playerid][pAdmin] < 4) return SendClientMessage(playerid, SIVAUB, "No");
	if(sscanf(params, "ii", id,slot)) return SendClientMessage(playerid, SIVAUB, "Use: /makeadminslot [id] [slot (1-20)]");

	level = PlayerInfo[id][pAdmin];


	base = db_open("base.db");

	format(query, sizeof(query), "UPDATE admins SET Name = %s WHERE Slot = %d", GetName(id),slot);


	db_query(base, query);
}



Re: Problem with SQL query - SymonClash - 18.03.2019

What you mean with "i cant get it to work"?

Also you should escape strings, use %e instead of %s.


Re: Problem with SQL query - Banditul18 - 18.03.2019

Quote:
Originally Posted by SymonClash
Посмотреть сообщение
What you mean with "i cant get it to work"?

Also you should escape strings, use %e instead of %s.
Thats SQLite not MySQL. %e not working with format but there is %q

OP: You need to use ' for strings

PHP код:
format(querysizeof(query), "UPDATE admins SET Name = '%q' WHERE Slot = %d"GetName(id),slot); 



Re: Problem with SQL query - Dolke - 19.03.2019

Thanks it works it updates database.

I have one more question.How could I list all admin names from database into dialog in game? How to get all of the admin names?


Re: Problem with SQL query - Banditul18 - 19.03.2019

Forgot to tell you something about your command actually:
PHP код:
YCMD:makeadminslot(playeridparams[], help
{
    new 
id,level,slot,query[512],DB:base;
    if(
PlayerInfo[playerid][pAdmin] < 4) return SendClientMessage(playeridSIVAUB"No");
    if(
sscanf(params"ii"id,slot)) return SendClientMessage(playeridSIVAUB"Use: /makeadminslot [id] [slot (1-20)]");
    
level PlayerInfo[id][pAdmin];
    
base db_open("base.db");
    
format(querysizeof(query), "UPDATE admins SET Name = '%q' WHERE Slot = %d"GetName(id),slot);
    
db_free_result(db_query(basequery));
    
db_close(base);

ALWAYS free the db_query, doesnt matter what type of query is. Memory leak is bad. Also i see you open a connection with database yet never close it this might lead to to many connections to database and lock the database

Aint expert in SQLite but i assume you can do something like this for dialog
PHP код:
YCMD:adminlist(playeridparams[], help
{
    new 
query[129],DB:baseDBResult:db_resultrowsadminName[MAX_PLAYER_NAME+1], adminList[165];
    
    
base db_open("base.db");
    
format(querysizeof(query), "SELECT Name FROM admins");
    
db_result db_query(basequery);
    
rows db_num_rows(db_result);
    if(!
rows)
        return 
SendClientMessage(playerid, -1"There is no admins!");
    
    for(new 
row 0rowsrow++)
    {
        
db_get_field(db_resultrowadminNamesizeof adminName);
        
strcat(adminListadminName);
        
strcat(adminList"\n");
    }
    
    
ShowPlayerDialog(playeriddialogidDIALOG_STYLE_LIST"Admin List"adminList"OK");
    
db_free_result(db_result);
    
db_close(base);

I think something like this. If im wrong someone correct me


Re: Problem with SQL query - Dolke - 19.03.2019

Okay I did like you wrote but I have one big problem.

When I type adminlist nothing happens,server freezes,none of the commands work.


I accidentally left server turned on,and I previously connected to server via bot (Raksamp).

When I came back (about 10-15 minutes later) dialog was opened and it listed admins.


EDIT: Okay I fixed It but now I have problem again. Now update does not work ( first command ).
I did not change anything since last night and now it does not work.


Re: Problem with SQL query - Dolke - 21.03.2019

Okay so update works ( for now )

But command for listing all admins from database is not working.It lists 5 rows in dialog (which is ok) but only one name and its level are listed.

Code: https://pastebin.com/AYAshVDT


EDIT: I found the solution,thanks all for helping