undefined Posted September 18, 2015 Share Posted September 18, 2015 Hi guys. I'm use it for create new column. But i have a problem. How can i set column data value when the column created? My code: dbExec(database, "ALTER TABLE table_name ADD column_name data_type") Thanks Link to comment
JR10 Posted September 18, 2015 Share Posted September 18, 2015 The default value? Append "DEFAULT 'VALUE'" to your column definition. Link to comment
undefined Posted September 18, 2015 Author Share Posted September 18, 2015 dbExec(database, "ALTER TABLE table_name ADD column_name data_type DEFAULT "..value) Like this? Link to comment
JR10 Posted September 18, 2015 Share Posted September 18, 2015 Yes. Make sure you actually want a default value, as in, when you insert a row without a value, it will be automatically set to the default value. Link to comment
undefined Posted September 18, 2015 Author Share Posted September 18, 2015 This code create column if the column is already exists. But I want to create column if not exists. How can i fix it? Edit: I tryed these but these give syntax error. dbExec(database, "ALTER TABLE IF NOT EXISTS table_name ADD column_name data_type DEFAULT "..value) dbExec(database, "ALTER TABLE table_name ADD IF NOT EXISTS column_name data_type DEFAULT "..value) Link to comment
undefined Posted September 18, 2015 Author Share Posted September 18, 2015 if not dbPoll(dbQuery(database, "SELECT column_name FROM table_name"), -1) then dbExec(database, "ALTER TABLE table_name ADD column_name data_type DEFAULT "..value) end I'm solved my problem with this code. It's add new column if the column not exists but it's give warning while adding column. Eventually it work! If you have not given warning code, I'll wait! Link to comment
JR10 Posted September 18, 2015 Share Posted September 18, 2015 What are you trying to achieve here? Altering a table should only occur once, I don't see the point of checking each time. The first line does not check if the column exists or not. It checks if results were returned (even if empty) or not. Link to comment
undefined Posted September 18, 2015 Author Share Posted September 18, 2015 But it is work. The first line check column result. If the column isn't exists it give warning and result is nil. So, it's work but only give warning if the column is not exists. Have you idea for check if column exists or not? If yes, can you help me? thx Link to comment
JR10 Posted September 19, 2015 Share Posted September 19, 2015 There are ways to check, but, I'm asking again, why? What are you trying to achieve here? You should just not alter tables in scripts, the table's schema should be static throughout. 1 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