我如何删除SQLite表列的Andr​​oid?SQLite、Andr、oid

2023-09-05 07:53:14 作者:渲染ˇ灯红酒绿-

我试图通过删除列如下

  openDB.execSQL(ALTER TABLE的收藏+DROP COLUMN favsCount);
 

LogCat中提供了以下信息:

  

11-07 21:18:29.238:ERROR /数据库(13952):失败1(近DROP:语法错误)。在0x34e550时preparingALTER TABLE的收藏DROP COLUMN favsCount

难道无法删除的源码领域为Android?

解决方案

抱歉,SQLite的不支持DROP COLUMN :

  

(11)如何在SQLite的添加或删除列从现有的表。

     

SQLite的有限ALTER TABLE支持,你可以用它来列添加到表的末尾或更改表的名称。 [...]

     

例如,假设你有一个名为T1的列名A,B表,C和你想从该表中删除列C。下面的步骤说明如何可以这样做:

  BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(A,B);
INSERT INTO t1_backup选择A,B从T1;
DROP TABLE T1;
CREATE TABLE T1(A,B);
INSERT INTO T1选择A,B FROM t1_backup;
DROP TABLE t1_backup;
承诺;
 

因此​​,基本上,你必须使用复制,删除表,创建新表,复制回的技术来删除列。

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?

解决方案

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.