(MySQL) Add column if not exists? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: (MySQL) Add column if not exists? (
/showthread.php?tid=660287)
(MySQL) Add column if not exists? -
ByMatt20030 - 29.10.2018
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
Re: (MySQL) Add column if not exists? -
TheToretto - 29.10.2018
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.
Re: (MySQL) Add column if not exists? -
ByMatt20030 - 29.10.2018
I understand but can you give me an example? (i'm using MySQL since 4 hours ago
)
Re: (MySQL) Add column if not exists? -
Jefff - 30.10.2018
https://stackoverflow.com/questions/...table-with-sql
Re: (MySQL) Add column if not exists? -
corne - 30.10.2018
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;
Re: (MySQL) Add column if not exists? -
TheToretto - 30.10.2018
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;
}
Re: (MySQL) Add column if not exists? -
ByMatt20030 - 30.10.2018
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)