我需要重命名SQLite数据库中一些表中的一些列。 我知道以前有人在stackoverflow上问过类似的问题,但这是针对一般的SQL,没有提到SQLite的情况。

从ALTER TABLE的SQLite文档中,我收集到不可能“轻松”地做这样的事情(即一个ALTER TABLE语句)。

我想知道有人知道一个通用的SQL方法做这样的事情与SQLite。


当前回答

修改表列< id >为< _id >

 String LastId = "id";

    database.execSQL("ALTER TABLE " + PhraseContract.TABLE_NAME + " RENAME TO " + PhraseContract.TABLE_NAME + "old");
    database.execSQL("CREATE TABLE " + PhraseContract.TABLE_NAME
    +"("
            + PhraseContract.COLUMN_ID + " INTEGER PRIMARY KEY,"
            + PhraseContract.COLUMN_PHRASE + " text ,"
            + PhraseContract.COLUMN_ORDER  + " text ,"
            + PhraseContract.COLUMN_FROM_A_LANG + " text"
    +")"
    );
    database.execSQL("INSERT INTO " +
            PhraseContract.TABLE_NAME + "("+ PhraseContract.COLUMN_ID +" , "+ PhraseContract.COLUMN_PHRASE + " , "+ PhraseContract.COLUMN_ORDER +" , "+ PhraseContract.COLUMN_FROM_A_LANG +")" +
            " SELECT " + LastId +" , "+ PhraseContract.COLUMN_PHRASE + " , "+ PhraseContract.COLUMN_ORDER +" , "+ PhraseContract.COLUMN_FROM_A_LANG +
            " FROM " + PhraseContract.TABLE_NAME + "old");
    database.execSQL("DROP TABLE " + PhraseContract.TABLE_NAME + "old");

其他回答

此问题已在2018-09-15(3.25.0)中修复

增强ALTER TABLE命令: 增加了使用ALTER table table RENAME COLUMN oldname TO newname重命名表中的列的支持。 修复表重命名特性,以便在触发器和视图中更新对重命名表的引用。

您可以在ALTER TABLE下面找到新的语法文档

RENAME COLUMN TO语法将表table-name的COLUMN -name更改为new-column-name。在表定义本身以及引用该列的所有索引、触发器和视图中更改列名。如果列名更改将导致触发器或视图中的语义歧义,则RENAME column将失败并报错,并且不会应用任何更改。

图片来源:https://www.sqlite.org/images/syntax/alter-table-stmt.gif

例子:

CREATE TABLE tab AS SELECT 1 AS c;

SELECT * FROM tab;

ALTER TABLE tab RENAME COLUMN c to c_new;

SELECT * FROM tab;

db-fiddle.com演示


Android支持

在撰写本文时,Android的API 27正在使用SQLite包3.19版本。

基于Android正在使用的当前版本,这个更新将在SQLite的3.25.0版本中发布,我想说的是,在Android添加此支持之前,您需要等待(大约API 33)。

而且,即使这样,如果你需要支持任何比API 33更老的版本,你也不能使用这个。

案例1:SQLite 3.25.0+

只有SQLite的3.25.0版本支持重命名列。如果你的设备满足这个要求,事情就很简单了。下面的查询可以解决你的问题:

ALTER TABLE "MyTable" RENAME COLUMN "OldColumn" TO "NewColumn";

案例2:SQLite旧版本

你必须遵循不同的方法来得到结果,这可能有点棘手

例如,如果你有一个这样的表:

CREATE TABLE student(Name TEXT, Department TEXT, Location TEXT)

如果您希望更改列Location的名称

步骤1:重命名原始表:

ALTER TABLE student RENAME TO student_temp;

步骤2:现在用正确的列名创建一个新表student:

CREATE TABLE student(Name TEXT, Department TEXT, Address TEXT)

步骤3:将原表中的数据复制到新表中:

INSERT INTO student(Name, Department, Address) SELECT Name, Department, Location FROM student_temp;

注意:上面的命令应该是一行。

步骤4:删除原来的表:

DROP TABLE student_temp;

通过这四个步骤,您可以手动更改任何SQLite表。 请记住,您还需要在新表上重新创建索引、查看器或触发器。

来自官方文件

对于某些不以任何方式影响磁盘上内容的更改,可以选择使用更简单、更快的过程。以下更简单的过程适用于删除CHECK或FOREIGN KEY或NOT NULL约束,重命名列,或添加、删除或更改列上的默认值。

Start a transaction. Run PRAGMA schema_version to determine the current schema version number. This number will be needed for step 6 below. Activate schema editing using PRAGMA writable_schema=ON. Run an UPDATE statement to change the definition of table X in the sqlite_master table: UPDATE sqlite_master SET sql=... WHERE type='table' AND name='X'; Caution: Making a change to the sqlite_master table like this will render the database corrupt and unreadable if the change contains a syntax error. It is suggested that careful testing of the UPDATE statement be done on a separate blank database prior to using it on a database containing important data. If the change to table X also affects other tables or indexes or triggers are views within schema, then run UPDATE statements to modify those other tables indexes and views too. For example, if the name of a column changes, all FOREIGN KEY constraints, triggers, indexes, and views that refer to that column must be modified. Caution: Once again, making changes to the sqlite_master table like this will render the database corrupt and unreadable if the change contains an error. Carefully test of this entire procedure on a separate test database prior to using it on a database containing important data and/or make backup copies of important databases prior to running this procedure. Increment the schema version number using PRAGMA schema_version=X where X is one more than the old schema version number found in step 2 above. Disable schema editing using PRAGMA writable_schema=OFF. (Optional) Run PRAGMA integrity_check to verify that the schema changes did not damage the database. Commit the transaction started on step 1 above.

引用sqlite文档:

SQLite支持的有限子集 ALTER TABLE。ALTER TABLE命令 在SQLite中允许用户重命名 表或添加新列到 现有的表。不能重命名列、删除列或从表中添加或删除约束。

当然,您可以用新的布局创建一个新表,SELECT * FROM old_table,并用您将收到的值填充新表。

修改表列< id >为< _id >

 String LastId = "id";

    database.execSQL("ALTER TABLE " + PhraseContract.TABLE_NAME + " RENAME TO " + PhraseContract.TABLE_NAME + "old");
    database.execSQL("CREATE TABLE " + PhraseContract.TABLE_NAME
    +"("
            + PhraseContract.COLUMN_ID + " INTEGER PRIMARY KEY,"
            + PhraseContract.COLUMN_PHRASE + " text ,"
            + PhraseContract.COLUMN_ORDER  + " text ,"
            + PhraseContract.COLUMN_FROM_A_LANG + " text"
    +")"
    );
    database.execSQL("INSERT INTO " +
            PhraseContract.TABLE_NAME + "("+ PhraseContract.COLUMN_ID +" , "+ PhraseContract.COLUMN_PHRASE + " , "+ PhraseContract.COLUMN_ORDER +" , "+ PhraseContract.COLUMN_FROM_A_LANG +")" +
            " SELECT " + LastId +" , "+ PhraseContract.COLUMN_PHRASE + " , "+ PhraseContract.COLUMN_ORDER +" , "+ PhraseContract.COLUMN_FROM_A_LANG +
            " FROM " + PhraseContract.TABLE_NAME + "old");
    database.execSQL("DROP TABLE " + PhraseContract.TABLE_NAME + "old");