.:HyPeX:. Posted February 23, 2015 Share Posted February 23, 2015 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
JR10 Posted February 24, 2015 Share Posted February 24, 2015 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: 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
Gallardo9944 Posted February 24, 2015 Share Posted February 24, 2015 Or you could simply create a column once (and for all) and forget about table's structure after that. Link to comment
.:HyPeX:. Posted February 27, 2015 Author Share Posted February 27, 2015 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: 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 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now