Inserting new value in the database (SQL - EasyDB)
#1

Hello!

It's been a while since I last requested some help on the forums, but I got to. I'm trying to learn working on dynamic systems and am currently working on a vehicle system. It's working pretty fine until now, but when I try to buy a new car, it's overwriting the old value.

http://pastebin.com/zsm68m3w

This is the code I use to create a new vehicle for the player, however it doesen't create new user, but edits the only one instead. I'm using EasyDB to manage the SQL system. The thing is, new row is being created BUT old value is being edited. So, new row with player name will be created, but the values that have to be edited (model and position) will be edited in the old row (first row that gets selected and that has the player name as "owner")



Any response is appreciated.
Reply
#2

Problem has been fixed. Had to create new functions that would help fix /my/ problem only.

pawn Код:
dbglobal bool:DB::SetLastIntEntry(table, const column[], value, const name[])
{
    if (! DB::Get())
    {
        return false;
    }

    new globQuery[2000];
   
    format(globQuery, sizeof globQuery, "UPDATE `%s` SET `%s` = '%d' WHERE `%s` = (SELECT `%s` FROM `%s` WHERE `Owner` = '%s' order by id desc LIMIT 1)", g_TableName[table], column, value, g_TableKey[table], g_TableKey[table], g_TableName[table], name);
    print(globQuery);
    new DBResult:result = db_query(g_Database, globQuery);
    if (result)
    {
        db_free_result(result);

        return true;
    }

    return false;
}

dbglobal bool:DB::SetLastFloatEntry(table, const column[], Float:value, const name[])
{
    if (! DB::Get())
    {
        return false;
    }

    new globQuery[2000];
   
    format(globQuery, sizeof globQuery, "UPDATE `%s` SET `%s` = '%f' WHERE `%s` = (SELECT `%s` FROM `%s` WHERE `Owner` = '%s' order by id desc LIMIT 1)", g_TableName[table], column, value, g_TableKey[table], g_TableKey[table], g_TableName[table], name);
    print(globQuery);
    new DBResult:result = db_query(g_Database, globQuery);
    if (result)
    {
        db_free_result(result);

        return true;
    }

    return false;
}

dbglobal bool:DB::SetLastStringEntry(table, const column[], const value[], const name[])
{
    if (! DB::Get())
    {
        return false;
    }
    new globQuery[2000];

    format(globQuery, globQuery, "UPDATE `%s` SET `%s` = '%q' WHERE `%s` = (SELECT `%s` FROM `%s` WHERE `Owner` = '%s' order by id desc LIMIT 1)", g_TableName[table], column, value, g_TableKey[table], g_TableKey[table], g_TableName[table], name);
    print(globQuery);
    new DBResult:result = db_query(g_Database, globQuery);
    if (result)
    {
        db_free_result(result);

        return true;
    }

    return false;
}
Those are adjusted to match my coding (tables and rows). It's suggested you modify them before you continue. The syntax is different as well, there's no more need to retrieve key as it's automatic now, just player name is required.

Код:
(table, const column[], value, const name[])

Table = table you're working on.
column = column you want to update
value = the value you want to update
name = the player's name

Example: DB::SetLastIntEntry(vTable, "Model", model, GetName(playerid));

This will set "Model" INT to model (script-defined), using vTable as a table and GetName as player name, in order to compare it with "Owner", column I use to identify vehicle owner in the code.
Could simply delete and modify main post, but as long as I didn't get any response, will be hard for others to do so as well. Hopefully this thread will help other people having the same issue.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)