How is it possible for a table to have multiple entries for a single primary key in sqlite3 ? This is how I identified the problem:
$ 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!]
UPDATES
A sample of duplicate entries was requested:
$ 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 is too short : The database is filled by a tcl script which runs every hour and uses one of the following queries to insert data :
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});
I just thought of something else, I don’t know if this can help… :
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..)
[...]
To answer your question directly:
It is only possible when there is an error with your primary key index.
The problem is shown by your integrity_check error, since sqlite3 uses this index to determine primary key uniqueness. When the index is bad you will be able to insert duplicate records
To solve: I would build and repopulate a new table. Remember when you copy the records you will need to deal with these duplicate records.
Also, you don’t show your sqlite3 version number, that would help in solving the problem.