简体   繁体   English

使用C#查询SQlite数据库时发生ConstraintException

[英]ConstraintException when querying SQlite database with C#

I'm hoping somebody will be able to help with my SQLite database problem. 我希望有人能够帮助解决我的SQLite数据库问题。

I'm receiving a ConstraintException when querying my SQLite database with C#. 在使用C#查询SQLite数据库时,我收到了一个ConstraintException The full exception message is “ Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. 完整的异常消息是“ Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. ” I originally built this database using access which worked fine, but for various reasons I had to recreate it using SQLite. “我最初使用访问权限构建了这个数据库,工作正常,但出于各种原因,我不得不使用SQLite重新创建它。

To give a bit of background - this is a simple status scheduling program. 为了给出一些背景知识 - 这是一个简单的状态调度程序。 Each Status has an associated Account and Schedule . 每个Status都有一个关联的AccountSchedule I realise Statuses and Schedule is a 1:1 relationship and could be in the same table but to allow the program to develop further I have split them into two tables. 我意识到StatusesSchedule是一个1:1的关系,可能在同一个表中,但为了让程序进一步发展,我将它们分成两个表。

See below for a cut down version of my table script (this is enough to recreate the problem). 请参阅下面的表格脚本的缩减版本(这足以重现问题)。

PRAGMA foreign_keys = ON;

CREATE TABLE Accounts
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name char(100));

CREATE TABLE Statuses
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
AccountId INTEGER REFERENCES Accounts(ID) ON DELETE CASCADE,
Text char(140));

CREATE TABLE Schedule
(ID INTEGER PRIMARY KEY REFERENCES Statuses(ID) ON DELETE CASCADE,
StartDate char(255),
Frequency INT);

I did not have any issues until I created two Statues and associated them to the same Account . 在我创建两个Statues并将它们关联到同一个Account之前,我没有遇到任何问题。

Accounts
ID  Name
1   Fred Blogs

Statuses
ID AccountId Text
1         1          “Some text”
2         1          “Some more text”

Schedule
ID    StartDate     Frequency
1     16/02/2011        1
2     16/02/2011        1

The select statement I'm using which throws the exception is: 我正在使用的抛出异常的select语句是:

SELECT Statuses.Id, Statuses.Text, Accounts.Id, Accounts.Name, Schedule.StartDate, Schedule.Frequency
FROM [Statuses], [Accounts], [Schedule]
WHERE Statuses.AccountId = Accounts.Id AND Statuses.Id = Schedule.Id

If I run the same query, but remove the ' Accounts.Id ' column the query works fine. 如果我运行相同的查询,但删除' Accounts.Id '列查询工作正常。

See below for the C# code I'm using but I don't think this is the problem 请参阅下面的我正在使用的C#代码,但我不认为这是问题所在

public DataTable Query(string commandText)
    {

        SQLiteConnection sqliteCon = new SQLiteConnection(ConnectionString);
        SQLiteCommand sqliteCom = new SQLiteCommand(commandText, sqliteCon);
        DataTable sqliteResult = new DataTable("Query Result");

        try
        {
            sqliteCon.Open();
            sqliteResult.Load(sqliteCom.ExecuteReader());
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            sqliteCon.Close();
        }

        return sqliteResult;

    }

Any help will be appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

the error is occuring due to the ID columns in Statuses table and Schedule table. 由于Statuses表和Schedule表中的ID列而发生错误。 If they are not important delete the columns from the two tables. 如果它们不重要,请从两个表中删除列。

I have found a way round this problem. 我找到了解决这个问题的方法。 If I select the AccountId from the Schedule table rather than the Accounts table there is no exception thrown. 如果我从Schedule表而不是Accounts表中选择AccountId,则不会抛出异常。 It seems I was unable to run a SELECT statement that contained two Unique primary key columns. 似乎我无法运行包含两个唯一主键列的SELECT语句。

So instead of 而不是

SELECT Statuses.Id, Statuses.Text, Accounts.Id, Accounts.Name, Schedule.StartDate, Schedule.Frequency
FROM [Statuses], [Accounts], [Schedule]
WHERE Statuses.AccountId = Accounts.Id AND Statuses.Id = Schedule.Id

I run 我跑

SELECT Statuses.Id, Statuses.Text, Statuses.AccountId, Accounts.Name, Schedule.StartDate, Schedule.Frequency
FROM [Statuses], [Accounts], [Schedule]
WHERE Statuses.AccountId = Accounts.Id AND Statuses.Id = Schedule.Id

I fixed the issue by reading the schema only at first, then cleared the constraints of the datatable and then read again the data. 我通过首先读取模式来解决问题,然后清除数据表的约束,然后再次读取数据。 like this : 像这样 :

DataSet DS = new DataSet();
mytable = new DataTable();
DS.Tables.Add(mytable);
DS.EnforceConstraints = false;
SQLiteCommand command = DBconnection.CreateCommand();
command.CommandText = "select * from V_FullView";
SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly);
mytable.Load(reader);
mytable.Constraints.Clear();
reader = command.ExecuteReader();
mytable.Load(reader);
reader.Close();

my V_FullView is a view of 4 different tables merged. 我的V_FullView是4个不同表合并的视图。 It seems that the constraints are the ones of the first merged table (name was unique on that one, but replicated a multiple of times in the view) 似乎约束是第一个合并表的约束(名称在该表上是唯一的,但在视图中复制了多次)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM