Delete a Certain Row Using Row ID
#1

So I was wondering if it possible to delete a certain row in a table not using an auto-increment column.
Like for example if I have 5 columns and I delete one dynamically in-game then it'd the id for it would be like this:

1
2
4
5

Assuming I have deleted ID 3 and I'd like to know if it's possible to delete a certain column, for example.
I have 1 2 4 5 and I use /delcolumn 3 - I want it to delete the column that is the third one so in this case it'd be the id of 4.

Now, that's what I want to do bassicly.
I just can't figure it out.
Reply
#2

i dident get your idea pretty well but .. Read me

and im sorry if i dident understand you ..
Reply
#3

Doing such things seems rather clumsy. Could you elaborate on what you're trying to achieve?
Reply
#4

I'll elaborate.

I have a dealership system, so I am trying to delete a dealership.
So I entered a row id(For example row number 3 - id=3) or an id(DsID - elaborated more below) and then I use a query which goes like this:
"DELETE FROM `playerrelated` WHERE `DsID` = %d, id);
id is bassicly what the player has entered, now ID is a row.
Like if I enter 0 it'd say that it was deleted but then because the DsID what I use to find the id of a dealership doesn't equal to what I entered it won't delete the row.

So it'd bassicly look for where DsID equals the ID entered instead of the row.
So it won't delete the row.

I really don't know how to explain it so I hope you understood it.
And btw in my previous explaination it was row not column, misstype.
Also DsID is an auto-increment.

Here's the code and if you'd like a picture aswell, I'd provide you with one:

pawn Код:
forward updateDS(id);
public updateDS(id) {
    mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");
    return 1;
}

forward destroyDEALERSHIP(playerid, id);
public destroyDEALERSHIP(playerid, id) {
    if(id > cache_get_row_count(dbHandle)) {
        return SendClientMessage(playerid, COLOR_ERROR, "[ ! ] Invalid DEALERSHIP ID.");
    }
    else if(id < cache_get_row_count(dbHandle)) {
        new query[128];
        mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", id);
        mysql_tquery(dbHandle, query);
       
        DestroyPickup(DS[id][DsIcon]);
        printf("Successfully deleted dealership id %d -- QUERY:\n%s", id, query);
        format(ZString, sizeof(ZString), "[ ! ] Successfully DELETED DealerShip ID:%d.", id);
        SendClientMessage(playerid, COLOR_SUCCESS, ZString);
        SetTimerEx("updateDS", 1250, false, "d", id);
        return 1;
    }
    return 1;
}
Reply
#5

Example
pawn Код:
mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");

forward loadDEALERSHIPS();
public loadDEALERSHIPS()
{
    if(cache_num_rows() > 0)
    {
        new i;
        while( i < cache_num_rows())
        {
            DS[id][DsSQLID] = cache_get_field_content_int(i, "id", dbHandle);
            //... your code ....
            i++
        }
        if(i > 0) g_TotalDealerShip = i
    }

}

forward deleteDEALERSHIP(playerid, id);
public deleteDEALERSHIP(playerid, id) {
    if(cache_affected_rows() > 0)
    {
        new str[128];
        format(str, sizeof str, "Successfully deleted dealership id %d (SQLID: %d)", id, DS[id][DsSQLID]);
        SendClientMessage(playerid, -1, str);
    }
    else
    {
        SendClientMessage(playerid, -1, "Nothing was deleted, invalid id");
    }
}

CMD:deldealership(playerid, params[])
{
    new id;
    if(sscanf(params, "i", id)) return SendClientMessage(playerid, -1, "/deldealership id");
    if(id > MAX_DEALERSHIP) return SendClientMessage(playerid, -1, "invalid id");
    mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", DS[id][DsSQLID]);
    mysql_tquery(dbHandle, query, "deleteDEALERSHIP", "ii", playerid, id);
}
Reply
#6

Quote:
Originally Posted by rickisme
Посмотреть сообщение
Example
pawn Код:
mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");

forward loadDEALERSHIPS();
public loadDEALERSHIPS()
{
    if(cache_num_rows() > 0)
    {
        new i;
        while( i < cache_num_rows())
        {
            DS[id][DsSQLID] = cache_get_field_content_int(i, "id", dbHandle);
            //... your code ....
            i++
        }
        if(i > 0) g_TotalDealerShip = i
    }

}

forward deleteDEALERSHIP(playerid, id);
public deleteDEALERSHIP(playerid, id) {
    if(cache_affected_rows() > 0)
    {
        new str[128];
        format(str, sizeof str, "Successfully deleted dealership id %d (SQLID: %d)", id, DS[id][DsSQLID]);
        SendClientMessage(playerid, -1, str);
    }
    else
    {
        SendClientMessage(playerid, -1, "Nothing was deleted, invalid id");
    }
}

CMD:deldealership(playerid, params[])
{
    new id;
    if(sscanf(params, "i", id)) return SendClientMessage(playerid, -1, "/deldealership id");
    if(id > MAX_DEALERSHIP) return SendClientMessage(playerid, -1, "invalid id");
    mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", DS[id][DsSQLID]);
    mysql_tquery(dbHandle, query, "deleteDEALERSHIP", "ii", playerid, id);
}
My code is very very similiar to yours.
The only thing I'll add is that sql-id thing, I guess that what was I supposed to do-.. It's just a 'cosmetic' (sort of) change but still helpful.
Thanks everyone.
I appreciate it.
Reply
#7

Here's my code, turns out this new code that you gave me works, it actually deletes it from the database.
It also won't destroy the pickup.
But when I attempt to buy a dealership it still says that there's a dealership there.
Instead of saying there isn't.

But it's deleted from the db.

Here's my whole code, let me know what's wrong here-.. You'll probably get a late response if you respond to me because i g2g.

pawn Код:
YCMD:destroydealerships(playerid, params[], help) {
    new id;
    if(!IsPlayerAdmin(playerid)) return 0;
    else {
        if(sscanf(params, "d", id)) return SendClientMessage(playerid, COLOR_NOTE, "[ ! ] SYNTAX: /DestroyDealership [DS-ID]");
        new query[128];
        mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", id);
        mysql_tquery(dbHandle, query, "destroyDEALERSHIP", "dd", playerid, id);
    }
    return 1;
}


forward destroyDEALERSHIP(playerid, id);
public destroyDEALERSHIP(playerid, id) {
    if(cache_affected_rows(dbHandle) > 0) {
        DestroyPickup(DS[id][DsIcon]);
        printf("Successfully deleted dealership id %d --", id);
        format(ZString, sizeof(ZString), "[ ! ] Successfully DELETED DealerShip ID:%d. (SQL-ROW-ID:%d)", id, DS[id][DsID]); //Thanks to 'rickisme' for helping me with this one.
        SendClientMessage(playerid, COLOR_SUCCESS, ZString);
        mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");
    } else return SendClientMessage(playerid, COLOR_ERROR, "[ ! ] Invalid DEALERSHIP ID.");
    return 1;
}
Reply
#8

This, id != DS[id][DsID]
id = in game id ( can start from 0 )
DS[id][DsID] = sql id ( can start from any number higher than 0 )
Example, in table dealerships, i have 1 row, and value in cloumn DsID is 2
i'm start loading dealerships table by use this code
pawn Код:
mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");
forward loadDEALERSHIPS();
public loadDEALERSHIPS()
{
    if(cache_num_rows() > 0)
    {
        new i;
        while( i < cache_num_rows())
        {
            DS[i][DsSQLID] = cache_get_field_content_int(i, "DsID", dbHandle);
            printf("DealerShip ID: %d (SQLID: %d) loaded", i, DS[i][DsSQLID]);
            i++
           
        }
    }

}
Server console will print
pawn Код:
DealerShip ID: 0 (SQLID: 2) loaded
So, if i use your code ( /DestroyDealership 0 )
pawn Код:
mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", id);
Query will like this :
pawn Код:
DELETE FROM `dealerships` WHERE `DsID` = 0
And guess what, result is
pawn Код:
[ ! ] Invalid DEALERSHIP ID.
cause there's no row have column DsID value = 0
but if i replace your code with this :
pawn Код:
mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", DS[id][DsID]);
Query will like this :
pawn Код:
DELETE FROM `dealerships` WHERE `DsID` = 2
Result will be :
pawn Код:
[ ! ] Successfully DELETED DealerShip ID: 0. (SQL-ROW-ID: 2)
Reply
#9

Mhm-.. I see.
So I need to use instead of the ID in this:

pawn Код:
mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", id);
with this?

pawn Код:
mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `dealerships` WHERE `DsID` = %d", DS[id][DsID]);
And another thing, what do I do with the pickup, I did as you said, yet it doesn't work.
I probably frustrate you because I do not understand, so sorry about that

pawn Код:
forward destroyDEALERSHIP(playerid, id);
public destroyDEALERSHIP(playerid, id) {
    if(cache_affected_rows(dbHandle) > 0) {
        DestroyPickup(DS[id][DsIcon]); //This part, it doesn't delete the pickup.
        printf("Successfully deleted dealership id %d --", DS[id][DsID]);
        format(ZString, sizeof(ZString), "[ ! ] Successfully DELETED DealerShip ID:%d. (SQL-ROW-ID:%d)", id, DS[id][DsID]); //Thanks to 'rickisme' for helping me with this one.
        SendClientMessage(playerid, COLOR_SUCCESS, ZString);
        mysql_tquery(dbHandle, "SELECT * FROM `dealerships`", "loadDEALERSHIPS");
    } else return SendClientMessage(playerid, COLOR_ERROR, "[ ! ] Invalid DEALERSHIP ID.");
    return 1;
}

forward loadDEALERSHIPS(); //Here's where I fetch all the ds info
public loadDEALERSHIPS() {

    for(new i = 0 ; i < cache_num_rows(dbHandle) ; i++) { // Dealership information - Fetched.

        DS[i][DsID] = cache_get_field_content_int(i, "DsID", dbHandle);
        DS[i][DsLocX] = cache_get_field_content_float(i, "DsLocX", dbHandle);
        DS[i][DsLocY] = cache_get_field_content_float(i, "DsLocY", dbHandle);
        DS[i][DsLocZ] = cache_get_field_content_float(i, "DsLocZ", dbHandle);
        DS[i][DsIcon] = cache_get_field_content_int(i, "DsIcon", dbHandle);

        DS[i][DsIcon] = CreatePickup(1239, 23, DS[i][DsLocX], DS[i][DsLocY], DS[i][DsLocZ], -1); //And here is where I set the icon

    }
    printf("[ OXIGEN's VEHICLES ] [ ! ] Loaded %d DEALERSHIPS", cache_num_rows(dbHandle));
    return 1;
}
Reply
#10

^ Yes, just replace it

I don't see anything wrong w pickup, but you don't need to store pickupid to database
If you still got problem with pickup, try using streamer to create/destroy pickup (CreateDynamicPickup, IsValidDynamicPickup, DestroyDynamicPickup)
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)