我需要重命名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年9月发布的3.25.0版本开始,您现在可以使用ALTER TABLE重命名列。

原来“创建新表和删除旧表”的答案如下。


假设你有一个表,需要将“colb”重命名为“col_b”:

首先创建一个临时名称的新表,该名称基于旧表定义,但使用了更新的列名:

CREATE TABLE tmp_table_name (
  col_a INT
, col_b INT
);

然后从原始表复制内容。

INSERT INTO tmp_table_name(col_a, col_b)
SELECT col_a, colb
FROM orig_table_name;

放下旧桌子。

DROP TABLE orig_table_name;

最后将临时表重命名为原来的表:

ALTER TABLE tmp_table_name RENAME TO orig_table_name;

不要忘记重新创建索引、触发器等。文档更全面地描述了这些陷阱和注意事项。

在BEGIN事务中封装所有这些;和提交;也可能是个好主意。

如前所述,有一个工具SQLite数据库浏览器,它可以做到这一点。Lyckily,这个工具保存用户或应用程序执行的所有操作的日志。执行此操作一次并查看应用程序日志,您将看到所涉及的代码。复制查询并根据需要粘贴。为我工作。希望这能有所帮助

自版本2018-09-15 (3.25.0) Sqlite支持重命名列

https://sqlite.org/changes.html

来自官方文件

对于某些不以任何方式影响磁盘上内容的更改,可以选择使用更简单、更快的过程。以下更简单的过程适用于删除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.

虽然确实没有ALTER COLUMN,但如果您只想重命名列、删除NOT NULL约束或更改数据类型,则可以使用以下命令集:

注意:这些命令有可能破坏数据库,因此请确保您有备份

PRAGMA writable_schema = 1;
UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';
PRAGMA writable_schema = 0;

您将需要关闭并重新打开连接,或者清空数据库以将更改重新加载到模式中。

例如:

Y:\> sqlite3 booktest  
SQLite version 3.7.4  
Enter ".help" for instructions  
Enter SQL statements terminated with a ";"  
sqlite> create table BOOKS ( title TEXT NOT NULL, publication_date TEXT NOT NULL);  
sqlite> insert into BOOKS VALUES ("NULLTEST",null);  
Error: BOOKS.publication_date may not be NULL  
sqlite> PRAGMA writable_schema = 1; 
sqlite> UPDATE SQLITE_MASTER SET SQL = 'CREATE TABLE BOOKS ( title TEXT NOT NULL, publication_date TEXT)' WHERE NAME = 'BOOKS';  
sqlite> PRAGMA writable_schema = 0;  
sqlite> .q  

Y:\> sqlite3 booktest  
SQLite version 3.7.4  
Enter ".help" for instructions  
Enter SQL statements terminated with a ";"  
sqlite> insert into BOOKS VALUES ("NULLTEST",null);  
sqlite> .q  

引用: 编译指示writable_schema 当打开此pragma时,数据库所在的SQLITE_MASTER表可以使用普通的UPDATE、INSERT和DELETE语句进行更改。警告:误用此pragma很容易导致数据库文件损坏。

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