System.Data.SQLite

Login
This project makes use of Eagle, provided by Mistachkin Systems.
Eagle: Secure Software Automation
Ticket Hash: 7e3fa93744b9e2949a09a80ccbe463725b2aa8e8
Title: Problem with 3 table query!
Status: Closed Type: Code_Defect
Severity: Severe Priority: Immediate
Subsystem: Data_Reader Resolution: Fixed
Last Modified: 2011-11-13 06:25:30
Version Found In: 1.0.66.0 and 1.0.76.0
Description:
Table "GunsAndAmmo"

 p GunID integer prim autoinvrement
 GunManufactor text,
 GunModel text,
 Division text,
 Factor text,

Table "Shooters"

 ShooterInternalID integer prim autoincrement,
 DataBirth integer,
 LastName text,
 FirstName text,

Table "ShooterGun"

 ShooterID integer prim not null,
 GunID integer prim not null,

The main quary im havin problems with are:

 "Select 
Shooters.FirstName, 
Shooters.LastName, 
ShooterGun.ShooterID, 
GunsAndAmmo.Division, 
GunsAndAmmo.Factor, 
GunsAndAmmo.GunManufactor, 
GunsAndAmmo.GunModel 
from 
Shooters, ShooterGun, GunsAndAmmo"

This shoudl return about 24 results currently but are only returning 3. If I remove "ShooterGun.ShooterID" from the query then i get the correct number of results but no. The same happens if I use "Shooters.ShooterInternalID" insted of "ShooterGun.ShooterID".

This is not a problem when using a different client tool.

<hr /><i>anonymous added on 2011-10-26 10:33:53 UTC:</i><br />
Use:<br>
SQLiteConnection cnn = new SQLiteConnection(dbConnection);<br>
cnn.Open();<br>
SQLiteCommand mycommand = new SQLiteCommand(cnn);<br>
mycommand.CommandText = sql;<br>
SQLiteDataReader reader = mycommand.ExecuteReader();<br>
dt.Load(reader);<br>
reader.Close();<br>
cnn.Close();<br>

<hr /><i>anonymous added on 2011-10-26 12:35:07 UTC:</i><br />
Problem seems to exist in 1.0.76.0 also

<hr /><i>anonymous added on 2011-10-26 12:46:02 UTC:</i><br />
values in tabels:<br>
"GunsAndAmmo" <br>
2, CZ, SP-01 Shaddow, Open<br>
3, CZ, 75, Production<br>
 
Table "Shooters" <br>
1, 19800506, Aanonsen, Eirik<br>
2, , Westol, Stein<br>

Table "ShooterGun" <br>
1, 3<br>
1, 2<br>
2, 2<br>
2, 3<br>

<hr /><i>anonymous added on 2011-10-26 12:47:12 UTC:</i><br />
Forgot one entry ( for making it 24 as the expected result)<br>
3, , Eiriksa, Aasuhds

<hr /><i>mistachkin added on 2011-10-26 14:02:31 UTC:</i><br />
<verbatim>
Have you tried using the latest System.Data.SQLite?  Version 1.0.66.0 was
released in April 2010 and quite a few things have been changed and/or fixed
since then.

Also, is the following SQL what you are doing?

CREATE TABLE GunsAndAmmo (
  GunID INTEGER PRIMARY KEY AUTOINCREMENT,
  GunManufactor TEXT,
  GunModel TEXT,
  Division TEXT,
  Factor TEXT
);

CREATE TABLE Shooters (
  ShooterInternalID INTEGER PRIMARY KEY AUTOINCREMENT,
  DataBirth INTEGER,
  LastName TEXT,
  FirstName TEXT
);

CREATE TABLE ShooterGun (
  ShooterID INTEGER NOT NULL,
  GunID INTEGER NOT NULL
);

SELECT Shooters.FirstName, Shooters.LastName,
       ShooterGun.ShooterID, GunsAndAmmo.Division,
       GunsAndAmmo.Factor, GunsAndAmmo.GunManufactor,
       GunsAndAmmo.GunModel
FROM Shooters, ShooterGun, GunsAndAmmo;


INSERT INTO GunsAndAmmo (GunID, GunManufactor, GunModel, Division, Factor)
VALUES(2, 'CZ', 'SP-01', 'Shaddow', 'Open');

INSERT INTO GunsAndAmmo (GunID, GunManufactor, GunModel, Division, Factor)
VALUES(3, 'CZ', 75, NULL, 'Production');

INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName)
VALUES(1, 19800506, 'Aanonsen', 'Eirik');

INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName)
VALUES(2, NULL, 'Westol', 'Stein');

INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName)
VALUES(3, NULL, 'Eiriksa', 'Aasuhds');

INSERT INTO ShooterGun (ShooterID, GunID) VALUES(1, 3);
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(1, 2);
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(2, 2);
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(2, 3);
</verbatim>


<hr /><i>mistachkin added on 2011-10-26 14:37:49 UTC:</i><br />
I'm not able to reproduce this issue with either version of System.Data.SQLite.  If you can provide more specific steps to get this issue to reproduce with the latest version of System.Data.SQLite, please re-open this ticket.


<hr /><i>anonymous added on 2011-10-28 08:56:56 UTC:</i><br />
CREATE TABLE GunsAndAmmo ( GunID INTEGER PRIMARY KEY AUTOINCREMENT, GunManufactor TEXT, GunModel TEXT, Division TEXT, Factor TEXT );

CREATE TABLE Shooters ( ShooterInternalID INTEGER PRIMARY KEY AUTOINCREMENT, DataBirth INTEGER, LastName TEXT, FirstName TEXT );

CREATE TABLE ShooterGun ( ShooterID INTEGER NOT NULL, GunID INTEGER NOT NULL, PRIMARY KEY (ShooterID, GunID ));
            
INSERT INTO GunsAndAmmo (GunID, GunManufactor, GunModel, Division, Factor) VALUES(2, 'CZ', 'SP-01', 'Shaddow', 'Open');<br>
INSERT INTO GunsAndAmmo (GunID, GunManufactor, GunModel, Division, Factor) VALUES(3, 'CZ', 75, NULL, 'Production');
              
INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName) VALUES(1, 19800506, 'Aanonsen', 'Eirik');<br>
INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName) VALUES(2, NULL, 'Westol', 'Stein');<br>
INSERT INTO Shooters (ShooterInternalID, DataBirth, LastName, FirstName) VALUES(3, NULL, 'Eiriksa', 'Aasuhds');<br>

INSERT INTO ShooterGun (ShooterID, GunID) VALUES(1, 3);<br>
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(1, 2);<br>
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(2, 2);<br>
INSERT INTO ShooterGun (ShooterID, GunID) VALUES(2, 3);<br>

SELECT Shooters.FirstName, Shooters.LastName, ShooterGun.ShooterID, GunsAndAmmo.Division, GunsAndAmmo.Factor, GunsAndAmmo.GunManufactor, GunsAndAmmo.GunModel <br>
FROM Shooters, ShooterGun, GunsAndAmmo;

With this the problem exist. Gets only 2 results back. Also under 1.0.76.0 version. The differance is the ShooterGun table where I defined the two collums as primary keys.

Thank you for looking on this problem and please point out if I am wrong about something.

<hr /><i>anonymous added on 2011-10-28 09:34:47 UTC:</i><br />
IF I create the ShooterGun table with <br>UNIQUE (ShooterID, GunID )<br> insted of <br>PRIMARY KEY (ShooterID, GunID )<br>, it works as expected. Is this some design feture? I'm a bit puzzeled. Thanx for all and any feedback

<hr /><i>mistachkin added on 2011-10-28 14:33:49 UTC:</i><br />
I've been able to reproduce the results you are seeing.  It appears that the built-in System.Data.DataTable class in the .NET Framework gets confused by the schema of your database.  I'm still investigating this issue; however, I'm not sure how much can be done since the results returned by the System.Data.SQLite components are indeed correct (i.e. they return 24 rows).


<hr /><i>mistachkin added on 2011-11-12 06:47:27 UTC:</i><br />
May be related to <a href="/index.html/artifact?ci=tip&filename=System.Data.SQLite/SQLiteKeyReader.cs&ln=160-172">this code fragment</a>.


<hr /><i>mistachkin added on 2011-11-13 06:00:47 UTC:</i><br />
Fixed by check-in [da3e0bfb53].  The root cause of this issue was treating a column as 'unique' in multi-table data reader results just because it was unique in the underlying database table.  For types of joins that can include multiple rows with the same value in that particular column (e.g. a cross join), this can cause serious problems.

<hr /><i>mistachkin added on 2011-11-13 06:25:30 UTC:</i><br />
Now checked into trunk [8fe2a401d6].  If possible, please test this fix in your environment and report back.  If you need a custom binary compiled to facilitate this testing, just ask.