how to add column if not exists in sqlite3 database
时间: 2023-03-05 12:36:18 浏览: 346
You can add a new column to an existing table in SQLite3 database by using the ALTER TABLE command with the ADD COLUMN clause. To check if the column already exists before adding it, you can use the PRAGMA statement to get the list of columns in the table.
Here's an example of how to add a new column "new_column" to the "my_table" table, only if it doesn't already exist:
```
PRAGMA table_info(my_table); -- check if the column already exists
-- if the column doesn't exist, add it to the table
ALTER TABLE my_table ADD COLUMN IF NOT EXISTS new_column TEXT;
```
The above code first uses the PRAGMA statement to get the list of columns in the "my_table" table. Then, it uses the ALTER TABLE command with the ADD COLUMN clause to add a new column "new_column" of type TEXT to the table, only if it doesn't already exist.
Note that you can change the name and data type of the column to fit your specific use case.
阅读全文