Jump to content

ATLER ADD IF NOT EXISTS


.:HyPeX:.

Recommended Posts

As the topic name says, how can i achive this?

The following statement wont work:

  
"ALTER TABLE table_name ADD IF NOT EXISTS g_achivements varchar(255);" 
  

If i just make it and the column already exists it will say duplicate column, how can i skip the error appeareance?

Greetz

HyPeX

PD: I'd also like to know how can i set a default value to the column.

Link to comment

There is no easy way to do this as far as I know, "IF NOT EXISTS" in your query is useless as well. The error (more like warning) is harmless, it doesn't stop your query or anything, you can just carry on without the need to worry whether it will output the warning or not.

Now, if you really want to do this, there are certainly some methods, one is PRAGMA. PRAGMA is used to query for internal data.

Here's the query:

PRAGMA table_info(`table_name`); 

A screenshot of the output:

B7UVsTF.png

cid is the column's id in the table, name is the column's name. That's all you need.

And here's how you can use it in your script:

dbQuery(callback, database, "PRAGMA table_info(`table_name`)") 
  
function callback(qh) 
 local column_exists = false 
 local result = dbPoll(qh, 0) 
 for index, row in pairs(result) do 
  if (row.name == "column_name") then 
   column_exists = true 
  end 
 end 
 if (not column_exists) then 
  --code to alter table 
 end 
end 
  

Please note that I've never tried this in MTA before, but it should work.

Link to comment
There is no easy way to do this as far as I know, "IF NOT EXISTS" in your query is useless as well. The error (more like warning) is harmless, it doesn't stop your query or anything, you can just carry on without the need to worry whether it will output the warning or not.

Now, if you really want to do this, there are certainly some methods, one is PRAGMA. PRAGMA is used to query for internal data.

Here's the query:

PRAGMA table_info(`table_name`); 

A screenshot of the output:

B7UVsTF.png

cid is the column's id in the table, name is the column's name. That's all you need.

And here's how you can use it in your script:

dbQuery(callback, database, "PRAGMA table_info(`table_name`)") 
  
function callback(qh) 
 local column_exists = false 
 local result = dbPoll(qh, 0) 
 for index, row in pairs(result) do 
  if (row.name == "column_name") then 
   column_exists = true 
  end 
 end 
 if (not column_exists) then 
  --code to alter table 
 end 
end 
  

Please note that I've never tried this in MTA before, but it should work.

Yeah, is basically the same as checking if the index exists, and if it does, then just do it.

I was doing this with dbExec on resource start, but a double query would be usless (In terms of performance, for the intention).

I guess i'll just carry on with the warning, thanks :D

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...