(MySQL) Add column if not exists?
#1

Well, the title explains itselfs...


I want to make an MySQL query to add a column to a table only if that column not exists trough a gamemode. And i don't know how.

Any idea/help? i'm using this plugin
Reply
#2

MySQL doesn't have the if exists function for that so what you'll have to do is:

Select the name of the column you're planning to create, if it returns a result, then the column exist, else you can go ahead and alter table.
Reply
#3

I understand but can you give me an example? (i'm using MySQL since 4 hours ago )
Reply
#4

https://stackoverflow.com/questions/...table-with-sql
Reply
#5

I would highly recommend using MariaDB instead of MySQL if you're able to, it works exactly the same but has more features and I believe has slightly better performance by default as well (and most importantly, works with the MySQL plugin).

In MariaDB doing this is as simple as
Код:
ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name tinyint(1) DEFAULT 0;
Reply
#6

There you go, if it exists, it'll return the value 1 which is true, else 0 which is false.

pawn Код:
CMD:alter(playerid, params[])
{
    new query[128];
    mysql_format(Database, query, sizeof(query), "SHOW COLUMNS FROM `sa-mp_test` LIKE 'column1'");
    mysql_tquery(Database, query, "ColumnCheck");
    return 1;
}

forward ColumnCheck();
public ColumnCheck()
{
    printf("It returned the value %d", cache_num_rows());
    return 1;
}
Reply
#7

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
There you go, if it exists, it'll return the value 1 which is true, else 0 which is false.

pawn Код:
CMD:alter(playerid, params[])
{
    new query[128];
    mysql_format(Database, query, sizeof(query), "SHOW COLUMNS FROM `sa-mp_test` LIKE 'column1'");
    mysql_tquery(Database, query, "ColumnCheck");
    return 1;
}

forward ColumnCheck();
public ColumnCheck()
{
    printf("It returned the value %d", cache_num_rows());
    return 1;
}
I love you <3



@corne I'll think about it


@Jefff Thank you too! (i understand your reply, but i didn't know how to implement it correctly)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)