I'm trying to use information from sqlite3_errmsg() to figure out what *kind* of SQLITE_CONSTRAINT happened.

Cruising the archives of this mailing list, I see past discussions about the desire for really complete error information, including the name of the specific constraint that failed (and ideally in a format which does not need to be parsed or is designed to be parsed). I add my vote for how cool that functionality would be. For now, I'm just looking for a way to distinguish between the major types of constraint violations, (unique, check, foreign key, etc).

I would welcome remarks from the SQLite developers on the code snippet below.

Specific questions:

(1) What is the likelihood of future changes to the phrasing of sqlite3_errmsg() strings?

(2) Is there any situation where (0 == strcmp(sqlite3_errmsg(db), "constraint failed")) and the violation was NOT a CHECK constraint?

(3)  Dare I hold out hope for extended error codes like the following?

#define SQLITE_CONSTRAINT_UNIQUE      (SQLITE_CONSTRAINT | (1<<8) )
#define SQLITE_CONSTRAINT_FOREIGN_KEY (SQLITE_CONSTRAINT | (2<<8) )
#define SQLITE_CONSTRAINT_CHECK       (SQLITE_CONSTRAINT | (3<<8) )
#define SQLITE_CONSTRAINT_NOT_NULL    (SQLITE_CONSTRAINT | (4<<8) )

My code snippet:

--------
if (SQLITE_CONSTRAINT == rc)
{
    const char* psz_errmsg = sqlite3_errmsg(psql);

    // We're on thin ice here.  sqlite doesn't make any promises about
// what the errmsg string will contain for various constraint violations. // Nonetheless, this works. If sqlite changes the errmsgs in the future,
    // this will break.  We've got test cases to detect this.

    if (strstr(psz_errmsg, "not unique"))
    {
        // TODO should occur at the end of the string
        ...
    }
    else if (strstr(psz_errmsg, "foreign key"))
    {
// TODO could check for exact strcmp match to "foreign key constraint failed"

        ...
    }
    else if (strstr(psz_errmsg, "constraint failed"))
    {
        // TODO could check for exact strcmp match to "constraint failed"
// TODO riskiest one, since sqlite doesn't say it was a check constraint // TODO but it seems to use a more specific phrase in all other cases except check constraints

        ...
    }
}
--------

Thanks!

--
E

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to