Jump to content

Create New Column (SQLite)


undefined

Recommended Posts

Posted

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

Posted

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.

Posted

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) 

Posted
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! :lol:

If you have not given warning code, I'll wait! :roll:

Posted

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.

Posted

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

Posted

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.

  • Like 1

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...