MYSQL Data problem
#4

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
You didn't give the code where you set the level to 4, can we see that as well?

Also a suggestion:
Make multiple smaller queries to only update the fields that change.
It has no use to ALWAYS update ALL fields when only 1 value changes.

pawn Код:
// Sets the admin-level of another player
COMMAND:setlevel(playerid, params[])
{
    // If the player has an insufficient admin-level (he needs level 6), exit the command
    if (APlayerData[playerid][AdminLevel] < 6) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Only admins level 6 and higher can use this command");

    // Setup local variables
    new OtherPlayer, Level, Msg[128], OldLevel, Query[128];

    // Split parameters
    if (sscanf(params, "ui", OtherPlayer, Level)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/setlevel <OtherPlayer> <AdminLevel>\"");

    // If the other player has an invalid ID (sscanf returns 65535 as playerid if the player cannot be found), exit the command
    if (OtherPlayer >= MAX_PLAYERS) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}That player is not online");
    // If the other player hasn't logged in yet, exit the command
    if (APlayerData[OtherPlayer][LoggedIn] == false) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}That player hasn't logged in properly yet");
    // If "Level" is invalid, exit the command
    if ((Level < 0) || (Level > 6)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Use admin-levels ranging from 0 to 6");

    // Get the old level of the other player
    OldLevel = APlayerData[OtherPlayer][AdminLevel];
    // If the level is still the same, exit the command
    if (OldLevel == Level) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Other player's level hasn't been changed");

    // Store the new level of the player and save it to MySQL
    APlayerData[OtherPlayer][AdminLevel] = Level;
    mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET AdminLevel = '%i' WHERE ID = '%i'", APlayerData[OtherPlayer][AdminLevel], APlayerData[OtherPlayer][SQLID]);
    mysql_tquery(SQL_db, Query, "", "");


    // Check if the player has been promoted or demoted
    if (OldLevel < Level)
        format(Msg, 128, "Player %s has been promoted to %s by %s: %s", APlayerData[OtherPlayer][PlayerName], AdminLevelName[Level], AdminLevelName[APlayerData[playerid][AdminLevel]], APlayerData[playerid][PlayerName]);
    if (OldLevel > Level)
        format(Msg, 128, "Player %s has been demoted to %s by %s: %s", APlayerData[OtherPlayer][PlayerName], AdminLevelName[Level], AdminLevelName[APlayerData[playerid][AdminLevel]], APlayerData[playerid][PlayerName]);
    SendClientMessageToAll(0x00FF00FF, Msg);

    // Let the server know that this was a valid command
    return 1;
}

// This function adds the given Money and Score values to the given player
Player_Reward(playerid, Money, Score)
{
    new Query[128];

    // Add the given Money and Score to the player's account
    APlayerData[playerid][PlayerMoney] = APlayerData[playerid][PlayerMoney] + Money;
    APlayerData[playerid][PlayerScore] = APlayerData[playerid][PlayerScore] + Score;

    // Update money and score for this player in the player's account in MySQL
    mysql_format(SQL_db, Query, sizeof(Query), "UPDATE playerdata SET Money = '%i', Score = '%i' WHERE ID = '%i'", APlayerData[playerid][PlayerMoney], APlayerData[playerid][PlayerScore], APlayerData[playerid][SQLID]);
    mysql_tquery(SQL_db, Query, "", "");

    return 1;
}
As you see here, I make small functions all over the place and they only update the required fields, not ALL of them.
The first is my /setlevel command, the second is a function which I can use to give the player some money and/or scorepoints.
That last function is called everywhere in the script where the player earns money or scorepoints.
Maintaining such a small query is not a problem, and as soon as it's written, you never have to edit it again, as it's doing whatever it is supposed to be doing.

You have to keep updating yours everytime you wanna save an additional field, and because of it's length, you can make mistakes easily.

Updating money doesn't require overwriting the adminlevel, or wanted level, or muted status, or my deaths/kills, or even my banned status, they don't relate to eachother at all.

Imagine that your table will grow ALOT (say to 150 fields), would you rather make one HUGE query to update all 150 fields when only 1 value changes and where the other 149 values just stay the same?

Another comparison:
If you type a big post onto this forum and you make a typo (wrong character or wrong word), would you retype the entire post to fix the typo, or just edit that one character or word to fix the typo?

I guess you choose the second one.

Making such huge queries puts extra stress on your database and harddrive when it's not even needed.
MySQL needs to break up your query in different parts, analyze it word by word, search for every required column, overwrite each field to finish your query.
All this takes time, and the longer your query, the longer it takes.

It can work much faster if you only update the fields that need to be updated.
Leave the rest alone, as it stays the same anyway.

Of course, you'll have multiple queries all over your script instead of only one huge function.
You might as well use files, which NEED to be overwritten completely when only 1 values changes (those might actually be faster compared to updating all fields everytime in a database).

A database is created because you can update whatever you want, when you want and to do it fast, because you have access to each field separately.
That's their main function.
If I'm Saving/Loading the data only when players Connect/Disconnect, Which what he does as I think, That isn't so bad, and for me it's easier than saving/loading one variable on every function/command.

If he's updating ALL of them on each function/command, that's another thing and of-course isn't good

Updating MySQL database: all data or in parts?
Reply


Messages In This Thread
MYSQL Data problem - by vassilis - 15.01.2015, 01:35
Respuesta: MYSQL Data problem - by Zume - 15.01.2015, 03:44
Re: MYSQL Data problem - by PowerPC603 - 15.01.2015, 09:57
Re: MYSQL Data problem - by xVIP3Rx - 15.01.2015, 10:06
Re: MYSQL Data problem - by PowerPC603 - 15.01.2015, 10:15
Re: MYSQL Data problem - by xVIP3Rx - 15.01.2015, 10:29
Re: MYSQL Data problem - by vassilis - 15.01.2015, 12:41
Re: MYSQL Data problem - by PowerPC603 - 15.01.2015, 12:54
Re: MYSQL Data problem - by vassilis - 15.01.2015, 12:59
Re: MYSQL Data problem - by PowerPC603 - 15.01.2015, 13:02

Forum Jump:


Users browsing this thread: 1 Guest(s)