首页 > 解决方案 > 如何使用 DB Browser SQLite 在列中添加批量值

问题描述

我有一个包含两列的表。一列“rewords”包含 12186 个单词。另一个名为“ID”的列是我新创建的列,所以它是空的。我想在 rewords 列中添加数字。从 1、2、3 到 12186 的数字。我尝试使用主键而不是 null 操作,但它扰乱了顺序,并且它的顺序变得不可预测,如 1、2、6、10、7、8、19 .... 如果我手动在字段中输入 1,2,3,那会太费时费力。以下是我想要的截图。我希望我可以在 ID 列的字段中输入 1 到 12186。

我还在查询选项卡中尝试了以下功能,但它不起作用并给出“接近“for”的错误:语法错误:for”

for(int i = 1; i<=12186; i++){
    UPDATE tbl SET ID = i WHERE index = i;
}

在此处输入图像描述

标签: sqldatabasesqlitedb-browser-sqlite

解决方案


你不需要循环你可以用一个查询来做到这一点

UPDATE tbl 
SET ID =  index 
WHERE index  <= 12186;

否则,如果您没有列索引..您需要在 id 列中使用自动增量值..您应该

您可以使用 SQLite Expert Personal 4 来做到这一点:

1) Select the table and then go to "Design" tab > "Columns" tab.

2) Click select the ID column name, and type INTEGER and Not Null > Ok.

3) Go to "Primary Key" tab in "Desgin tab". 
      Click "Add" and select the column ID. Check the "Autoincrement" box.

4) Click "Apply" on the right bottom part of the window.

推荐阅读