Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete column from sqlite table in android?

Tags:

I tried deleting a column by using the following

openDB.execSQL("ALTER TABLE favs" + " DROP COLUMN favsCount"); 

LogCat gives the following message:

11-07 21:18:29.238: ERROR/Database(13952): Failure 1 (near "DROP": syntax error) on 0x34e550 when preparing 'ALTER TABLE favs DROP COLUMN favsCount'.

Is it not possible to delete fields in sqlite for Android?

like image 972
Abhishek Sharma Avatar asked Nov 08 '11 02:11

Abhishek Sharma


People also ask

How do you delete a column from a table in sqlite3?

DB Browser for SQLite allows you to add or drop columns. In the main view, tab Database Structure , click on the table name. A button Modify Table gets enabled, which opens a new window where you can select the column/field and remove it.

How do you delete a column from a table?

Right-click the column you want to delete and choose Delete Column from the shortcut menu. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.

How do I remove something from a table in SQLite?

sqlite> DELETE FROM table_name; Following is the basic syntax of DROP TABLE. sqlite> DROP TABLE table_name; If you are using DELETE TABLE command to delete all the records, it is recommended to use VACUUM command to clear unused space.

What is the command to add or delete columns in SQLite?

The syntax to ADD A COLUMN in a table in SQLite (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition; table_name.


2 Answers

Sorry, SQLite doesn't support DROP COLUMN:

(11) How do I add or delete columns from an existing table in SQLite.

SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. [...]

For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:

BEGIN TRANSACTION; CREATE TEMPORARY TABLE t1_backup(a,b); INSERT INTO t1_backup SELECT a,b FROM t1; DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup; DROP TABLE t1_backup; COMMIT; 

So basically, you have to use the "copy, drop table, create new table, copy back" technique to remove a column.

like image 97
mu is too short Avatar answered Sep 17 '22 12:09

mu is too short


as mu is too short says Sqlite doesn't allow to do an alter table to delete a column. here you can see the alter syntax definition

like image 22
Jordi Coscolla Avatar answered Sep 17 '22 12:09

Jordi Coscolla