I want to delete or add column in sqlite database
I am using following query to delete column.
ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME
But it gives error
System.Data.SQLite.SQLiteException: SQLite error
near "DROP": syntax error
I want to delete or add column in sqlite database
I am using following query to delete column.
ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME
But it gives error
System.Data.SQLite.SQLiteException: SQLite error
near "DROP": syntax error
ALTER TABLE SQLite
You can:
As others have pointed out
source : http://www.sqlite.org/lang_altertable.html
While you can always create a new table and then drop the older one. I will try to explain this workaround with an example.
Now you want to remove the column
height
from this table.Create another table called
new_person
Now copy the data from the old table
Now Drop the
person
table and renamenew_person
toperson
So now if you do a
.schema
, you will seeAs an alternative:
If you have a table with schema
you can use a
CREATE TABLE...AS
statement likeCREATE TABLE person2 AS SELECT id, first_name, last_name, age FROM person;
, i.e. leave out the columns you don't want. Then drop the originalperson
table and rename the new one.Note this method produces a table has no PRIMARY KEY and no constraints. To preserve those, utilize the methods others described to create a new table, or use a temporary table as an intermediate.
You can use the SQlite Administrator for changing the column names. Right Click on Table name and select Edit Table.Here you will find the table structure and you can easily rename it.
example to add a column:-
here student is table_name and TOB is column_name to be added.
It is working and tested.
I have improved user2638929 answer and now it can preserves column type, primary key, default value etc.
PS. I used here
android.arch.persistence.db.SupportSQLiteDatabase
, but you can easyly modify it for useandroid.database.sqlite.SQLiteDatabase