gpt4 book ai didi

android - 将唯一列添加到已存在的 sqlite 表

转载 作者:行者123 更新时间:2023-11-29 00:04:16 24 4
gpt4 key购买 nike

我在 Android 应用程序中使用 SQLite 数据库,我想知道如何将唯一列添加到已创建的表中。我的意思是存在表的唯一性由 2 个值确定,我想编辑表的唯一性由 3 个值确定(2 个存在,另外 1 个)。

这可能吗?

最佳答案

writable_schema trick不适用于 UNIQUE,因为需要更改内部索引。

进行此更改的唯一方法是使用表的临时副本:

CREATE TABLE NewTable(
[...],
UNIQUE(Col1, Col2, Col3)
);
INSERT INTO NewTable SELECT * FROM MyTable;
DROP TABLE MyTable;
ALTER TABLE NewTable RENAME TO MyTable;

(这应该包含在事务中;Android 的 onUpdate 会自动执行此操作。)

关于android - 将唯一列添加到已存在的 sqlite 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com