且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLite-更改表的列类型?

更新时间:2023-12-01 13:46:34

SQLite不完全支持ALTER TABLE语句.您唯一可以做的就是重命名表和/或添加列.如果要重命名列,***的选择是使用新列的数据类型/名称创建一个新表,并删除旧表以重命名新表.

SQLite does not fully support ALTER TABLE statements. The only thing you can do is rename a table and/or add columns. If you want to rename a column, your best option is to create a new table with the new columns datatype/names, and to drop the old table in order to rename the new one.

让我们说,您有一个表,需要将"field-1"重命名为"field-2": 首先== >>重命名旧表:

Lets say, you have a table and need to rename "field-1" to "field-2": First ==>> rename the old table:

    ALTER TABLE original RENAME TO tmp;

现在基于旧表创建新表,但使用更新的列名:
== >>用更新的列创建一个表

Now create the new table based on the old table but with the updated column name:
==>> create a table with the updated columns

    CREATE TABLE original(
    field_a INT
   , field_b INT
    );

然后从原始表中复制内容.

Then copy the contents across from the original table.

   INSERT INTO origignal(field_a, field_b)
   SELECT field_a, field_b
   FROM tmp;

最后,放下旧桌子.

   DROP TABLE tmp;