So, I don't want to ALTER my table if it's already altered previously. In my WPF app, I got a button for creating new empty table, then with another button I'm ALTERing the table, in that process I'm adding new columns to the empty table. After that I'm inserting data to the columns. Now my problem is, when I click that button again, it wants to alter the table again, before the instert, but throwing an error since the columns already exists.
What I want is to skip the "alter table" when the columns already exists. This is my base code which works for one click:
public bool updateTable(string tableNamee, string question, string Atype)
{
try
{
string query = $"ALTER TABLE appdb.{tableNamee} ADD question VARCHAR(100) NOT NULL AFTER questionID, ADD Atype VARCHAR(20) NOT NULL AFTER question; ";
string query2 = $"INSERT INTO appdb.{tableNamee} (question, Atype) VALUES('{question}','{Atype}'); ";
etc
This is what I tried:
public bool updateTable(string tableNamee, string question, string Atype)
{
try
{
string q = $"IF NOT EXISTS( SELECT NULL FROM appdb.{tableNamee} WHERE table_name = {tableNamee} AND table_schema = appdb " +
$"AND column_name = question, Atype) THEN ALTER TABLE appdb.{tableNamee} ADD question VARCHAR(100) NOT NULL AFTER questionID, ADD Atype VARCHAR(20) NOT NULL AFTER question; END IF; ";
string query2 = $"INSERT INTO appdb.{tableNamee} (question, Atype) VALUES('{question}','{Atype}'); ";
etc
But I'm getting the following error: "You have an error in your SQL syntax" I tired to combine the code in many variations but I can't see what is the problem in syntax. Can someone help me with this?