2011-06-22 28 views
3

如何为sqlite3中的单个主键创建多个条目?这是我发现的问题:如何为sqlite3中的单个主键拥有多个条目?

$ sqlite3 dbName.db 
sqlite> .s 
CREATE TABLE 'tableName' (
    columnOne INTEGER NOT NULL, 
    columnTwo INTEGER NOT NULL, 
    columnThree INTEGER NOT NULL, 
    columnFour INTEGER NOT NULL, 
    columnFive REAL NOT NULL, 
    PRIMARY KEY (columnOne, columnTwo, columnThree, columnFour) 
); 
sqlite> SELECT count(1) AS nb FROM tableName GROUP BY columnOne, columnTwo, columnThree, columnFour HAVING nb > 1; 
[A whole bunch of results, some with nb up to 34!] 

最新通报 被要求重复条目的示例:

$ sqlite3 observation.db 
sqlite> .mode column 
sqlite> .s 
CREATE TABLE 'observation' (
    station INTEGER NOT NULL, 
    specie INTEGER NOT NULL, 
    isAvg INTEGER NOT NULL, 
    date INTEGER NOT NULL, 
    value REAL NOT NULL, 
    PRIMARY KEY (station, specie, isAvg, date) 
); 
sqlite> SELECT * FROM observation WHERE station = 105001 AND specie = 3 AND isAvg = 1 AND date = 1308650400; 
station  specie  isAvg  date  value  
---------- ---------- ---------- ---------- ---------- 
105001  3   1   1308650400 31.0  
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 
105001  3   1   1308650400 2.42523266 

@mu太短:数据库是由运行Tcl脚本充满每小时使用以下查询之一插入数据:

INSERT OR REPLACE INTO observation (station, specie, isAvg, date, value) VALUES ($stationId, $speciesId, 0, $date, $value); 

INSERT OR REPLACE INTO observation (station, specie, isAvg, date, value) VALUES (${stationId}, ${speciesId}, 1, ${date}, ${speciesAvg}); 

我刚想到别的,我不知道这是否可以帮助...:

sqlite3 observation.db 
sqlite> pragma integrity_check; 
integrity_check 
rowid 53202997 missing from index sqlite_autoindex_observation_1 
rowid 53202998 missing from index sqlite_autoindex_observation_1 
rowid 53202999 missing from index sqlite_autoindex_observation_1 
rowid 53203000 missing from index sqlite_autoindex_observation_1 
rowid 53203006 missing from index sqlite_autoindex_observation_1 
rowid 53584951 missing from index sqlite_autoindex_observation_1 
[...] 
and more of the same (100 such lines since integrity_check stops at 100 by default..) 
[...] 
+0

@mu太短:我编辑了我的原始问题以添加重复数据样本和用于填充数据库的查询。 – Shawn

+0

@mu太短:它发生一次随机,而不是另一次,而我正在做很多测试,运行一个使用数据库的特定脚本,看看如果数据库不存在或为空会发生什么......除了这两次,它已经不可能重现的错误..至于数据库损坏,这是非常可能的,因为我们有时最终会出现像“数据库磁盘映像格式错误”或“磁盘I/O错误”的错误..问题是,我们不完全知道这些错误来自哪里。 – Shawn

+0

不,我还没有考虑过写作。我想我应该..谢谢亩,我会在这里发表评论,如果有进展。 – Shawn

回答

2

直接回答你的问题:
只可能在有你的主键索引错误。

由于sqlite3使用此索引来确定主键唯一性,您的integrity_check错误会显示此问题。当索引不好时,您可以插入重复记录

解决方法:我将构建并重新填充一个新表。请记住,当您复制记录时,您需要处理这些重复记录。

此外,您不显示您的sqlite3版本号,这将有助于解决问题。

相关问题