Marcus
Marcus

Reputation: 690

Check SQLiteException "extended" ResultCode in C# CLI App

i write an CLI App with SQLite support. For this i use the NuGet Pack "System.Data.SQLite" from SQLite Development Team. All works fine, but how can ckeck the "Extended Result Codes (Look at point 5)" in case of exception?

I can access all error codes in Enum SQLiteErrorCode but in "ex.ResultCode" is allways a Primary Result Code (Look at point 4).

e.g.

try
{
    // DB actions ....
}
catch (SQLiteException ex)
{
    // This is what i want because is clear and easy to read
    if (ex.ResultCode == SQLiteErrorCode.Constraint_Unique)
    {
        Debug.Write("SQLiteError: " + ex.Message);
    }

    // This works, but is not nice
    if (ex.ResultCde == SQLiteErrorCode.Constraint && ex.Message.Contains("UNIQUE"))
    {
        Debug.Write("SQLiteError: " + ex.Message);
    }

    throw ex;
}

Can anybody help me for this?

Upvotes: 1

Views: 505

Answers (1)

Lahna200
Lahna200

Reputation: 551

The feature was added since the version 1.0.70.0 :

https://system.data.sqlite.org/index.html/doc/trunk/www/news.wiki

1.0.70.0 - April 22, 2011

Added support for sqlite3_extended_result_codes(), sqlite3_errcode(), and sqlite3_extended_errcode() via SetExtendedResultCodes(), ResultCode(), and ExtendedResultCode(). Added support for SQLITE_CONFIG_LOG via SQLiteLogEventHandler().

To use it:

conn.Open();
conn.SetExtendedResultCodes(true);

and you can catch the exception:

if (ex.ResultCode == SQLiteErrorCode.Constraint_Unique)
{
Debug.Write("SQLiteError: " + ex.Message);
}

Upvotes: 1

Related Questions