SqliteException is thrown when a SQLite error is encountered. The message is provided by SQLite. The
SqliteExtendedErrorCode properties contain the SQLite result code of the error.
Errors may be encountered any time the Microsoft.Data.Sqlite interacts with the native SQLite library. The following list shows the common scenarios where errors can occur:
- Opening a connection.
- Beginning a transaction.
- Executing a command.
- Calling NextResult.
Consider carefully how your app will handle these errors.
Locking, retries, and timeouts
SQLite is aggressive when it comes to locking tables and database files. If your app enables any concurrent database access, you'll likely encounter busy and locked errors. You can mitigate many errors by using write-ahead logging.
Whenever Microsoft.Data.Sqlite encounters a busy or locked error, it will automatically retry until it succeeds or the command timeout is reached.
You can increase the timeout of a command by setting CommandTimeout. The default timeout is 30 seconds. A value of
0 means no timeout.
// Retry for 60 seconds while locked command.CommandTimeout = 60;
Microsoft.Data.Sqlite sometimes needs to create an implicit command object. For example, during BeginTransaction. To set the timeout for these commands, use DefaultTimeout.
// Set the default timeout of all commands on this connection connection.DefaultTimeout = 60;