PDA

View Full Version : Problem with SQL query


Dolke
18/03/2019, 05:44 PM
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);
}

SymonClash
18/03/2019, 08:49 PM
What you mean with "i cant get it to work"?

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

Banditul18
18/03/2019, 08:53 PM
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

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

Dolke
19/03/2019, 05:59 AM
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?

Banditul18
19/03/2019, 03:13 PM
Forgot to tell you something about your command actually:
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 = '%q' WHERE Slot = %d", GetName(id),slot);

db_free_result(db_query(base, query));

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
YCMD:adminlist(playerid, params[], help)
{
new query[129],DB:base, DBResult:db_result, rows, adminName[MAX_PLAYER_NAME+1], adminList[165];

base = db_open("base.db");

format(query, sizeof(query), "SELECT Name FROM admins");
db_result = db_query(base, query);

rows = db_num_rows(db_result);

if(!rows)
return SendClientMessage(playerid, -1, "There is no admins!");

for(new row = 0; i < rows; row++)
{
db_get_field(db_result, row, adminName, sizeof adminName);
strcat(adminList, adminName);
strcat(adminList, "\n");
}

ShowPlayerDialog(playerid, dialogid, DIALOG_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

Dolke
19/03/2019, 08:21 PM
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.

Dolke
21/03/2019, 11:57 PM
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 :)