DROP Syntax error in SQLiteManager

12.7k views Asked by At

I have downloaded sqlite manager app in Firefox browser and open sqlite manager app.

1) Created database named DBSQLTEST.

2) Created table named SQLTEST contain has threes fields are SLNO, NAME and AGE

3) Inserted new records

But i want remove a ‘AGE’ column in sqltest table

i using sql command like below

ALTER TABLE SQLTEST DROP COLUMN AGE

SQLiteManager message says

SQLiteManager: Likely SQL syntax error: ALTER TABLE SQLTEST DROP COLUMN AGE [ near "DROP": syntax error ] Exception Name: NS_ERROR_FAILURE Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

What is error that?

3

There are 3 answers

3
laalto On BEST ANSWER

sqlite does not support DROP COLUMN in ALTER TABLE. You can only rename tables and add columns.

Reference: http://www.sqlite.org/lang_altertable.html

If you need to remove columns, create a new table, copy the data there, drop the old table and rename the table to its intented name.

0
Toseef Khilji On

SQLite does not fully support ALTER TABLE statements. You can only rename table, or add columns.

If you want to drop a column, your best option is to create a new table without the column, and to drop the old table in order to rename the new one.

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;

Credits to Benoit.

0
Taj rajan On

Create a table

CREATE TABLE SQLTEST ("SLNO" INTEGER PRIMARY KEY ,"NAME" TEXT,AGE INTEGER)

If want to remove a column

ALTER TABLE SQLTEST RENAME TO SQLTESTCOPY

CREATE TABLE SQLTEST ("SLNO" INTEGER PRIMARY KEY ,"NAME" TEXT)

INSERT INTO SQLTEST"SELECT "SLNO","NAME" FROM SQLTESTCOPY

DROP TABLE SQLTESTCOPY

easy.