添加order子句时,SQLITE Query会产生不同的(更多)结果

时间:2014-02-12 13:10:55

标签: sqlite

如果没有深入了解底层结构,有没有人经历过这样的事情?

我在一张桌子上做一个选择*,结果是1187行 我做同样的选择并添加一个订单子句(在任何字段上)并获得1195行 编辑:这不是真的!只对displayname和id进行排序会带回缺失的结果。这些显然被索引了!

我可以使用这个特定的数据库重现行为,它在我的软件中混淆了一些功能,我在运行一些查询时没有对结果进行排序(因为顺序并不重要)

这可能是什么原因?我将提供更多信息作为nessecary,但底层结构相当复杂。

编辑:好的,表格如下:

id GUID PRIMARY KEY NOT NULL, 
tag TEXT,
displayname TEXT,
create_user GUID,
create_time INTEGER,
key_date DATETIME,
change_time INTEGER

我看过“缺失”的线条,并没有看到任何特别的事情。

这是integrity_check的结果:

*** in database main ***
On tree page 1974 cell 8: 2nd reference to page 62381
On tree page 1974 cell 8: Child page depth differs
On tree page 1973 cell 1: Child page depth differs
On tree page 1973 cell 2: Child page depth differs
On tree page 8931 cell 5: 2nd reference to page 57003
On tree page 8931 cell 5: Child page depth differs
On tree page 8931 cell 6: Child page depth differs
On tree page 1981 cell 8: 2nd reference to page 62380
On tree page 1981 cell 8: Child page depth differs
On tree page 1976 cell 1: Child page depth differs
On tree page 1976 cell 2: Child page depth differs
On tree page 8927 cell 5: 2nd reference to page 57001
On tree page 8927 cell 5: Child page depth differs
On tree page 8927 cell 6: Child page depth differs
On tree page 62331 cell 5: 2nd reference to page 57915
On tree page 62331 cell 5: Child page depth differs
On page 62331 at right child: 2nd reference to page 57828
On tree page 26340 cell 3: Child page depth differs
On tree page 26340 cell 4: Child page depth differs
On tree page 26341 cell 4: 2nd reference to page 49345
On tree page 26341 cell 4: Child page depth differs
On tree page 26341 cell 5: Child page depth differs
On tree page 58249 cell 0: 2nd reference to page 57717
On tree page 58249 cell 1: 2nd reference to page 57711
On tree page 58249 cell 2: 2nd reference to page 57700
On tree page 58249 cell 3: 2nd reference to page 5941
On tree page 58249 cell 4: Child page depth differs
On tree page 59501 cell 3: 2nd reference to page 56719
On tree page 59501 cell 3: Child page depth differs
On tree page 59501 cell 4: Child page depth differs
On tree page 54755 cell 1: Rowid 11 out of order (max larger than parent max of 10)
On tree page 54852 cell 1: Rowid 11 out of order (min less than parent min of 11)
On tree page 60653 cell 0: 2nd reference to page 58668
On tree page 60653 cell 1: Child page depth differs
On tree page 3834 cell 6: 2nd reference to page 62279
On tree page 3834 cell 6: Child page depth differs
On tree page 3834 cell 7: Child page depth differs
On tree page 60737 cell 4: 2nd reference to page 58694
On tree page 60737 cell 4: Child page depth differs
On tree page 4632 cell 9: Child page depth differs
On tree page 33958 cell 1: Child page depth differs
On tree page 7205 cell 0: 2nd reference to page 7204
On tree page 7205 cell 1: 2nd reference to page 7256
On tree page 7205 cell 2: Child page depth differs
Page 5033 is never used
Page 54385 is never used
Page 54481 is never used
Page 54582 is never used
Page 54639 is never used
Page 54687 is never used
Page 54785 is never used
Page 54848 is never used
Page 54849 is never used
Page 54850 is never used
Page 54873 is never used
Page 55669 is never used
Page 55762 is never used
Page 55871 is never used
Page 55920 is never used
Page 55979 is never used
Page 56178 is never used
Page 56223 is never used
Page 56224 is never used
Page 56227 is never used
Page 56237 is never used
Page 56243 is never used
Page 56245 is never used
Page 56248 is never used
Page 56261 is never used
Page 56377 is never used
Page 56824 is never used
Page 57261 is never used
Page 57266 is never used
Page 57303 is never used
Page 57325 is never used
Page 57393 is never used
Page 57424 is never used
Page 57473 is never used
Page 57835 is never used
Page 57847 is never used
Page 57874 is never used
Page 57875 is never used
Page 57887 is never used
Page 57893 is never used
Page 57917 is never used
Page 57918 is never used
Page 57919 is never used
Page 57923 is never used
Page 57932 is never used
Page 59220 is never used
Page 59224 is never used
Page 59226 is never used
Page 59228 is never used
Page 59229 is never used
Page 59230 is never used
Page 59231 is never used
Page 59232 is never used
Page 59235 is never used
Page 59238 is never used
Page 59249 is never used

1 个答案:

答案 0 :(得分:0)

数据库文件已损坏。

这可能以各种方式发生,但是电源故障或类似的灾难性故障与未写入的缓冲区可能会这样做。

您需要构建一个新的数据库文件。

请按照以下步骤操作:

  1. 制作当前数据库的备份副本(这样如果你把它弄得比现在更糟,你总是可以恢复到腐败状态)。
  2. 转储当前数据库的内容:

    sqlite3 test.db
    > .mode insert
    > .output test.sql
    > .dump
    

    这可能需要一段时间

  3. 创建新数据库并导入转储:

    sqlite3 new.db
    > .read test.sql
    
  4. 验证新数据库的完整性:

    > pragma integrity_check;
    ok
    
  5. 使用新数据库而不是旧数据库

  6. 注意,可能在此过程中丢失数据,但数据库 已损坏,不应继续使用