Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sqlite3中,一个表如何可能对一个主键有多个条目?_Sqlite_Primary Key - Fatal编程技术网

在sqlite3中,一个表如何可能对一个主键有多个条目?

在sqlite3中,一个表如何可能对一个主键有多个条目?,sqlite,primary-key,Sqlite,Primary Key,在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

在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..)
[...]

直接回答您的问题:
只有当主键索引出现错误时才有可能执行此操作。

由于sqlite3使用此索引来确定主键唯一性,因此完整性检查错误显示了问题。当索引不正确时,您将能够插入重复的记录

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


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

@mu太短:我编辑了原始问题,添加了重复数据样本和用于填充数据库的查询。@mu太短:它随机发生过一次,还有一次,我做了很多测试,运行一个使用数据库的脚本,看看如果数据库不存在或者是空的,会发生什么。。除了这两次,还不可能重现该错误。。至于数据库损坏,这是很有可能的,因为我们有时会出现诸如“数据库磁盘映像格式错误”或“磁盘I/O错误”之类的错误。。问题是,我们也不知道这些错误是从哪里来的。不,我还没有考虑过写作。我想我应该。。谢谢穆,如果有进展,我会在这里发表评论。