[Solved] SQLite Error Database is Locked – Error Code 5

This is the most frequent error that SQLite users normally encounter. In this article we will explain the easiest and manual method of fixing the error “SQLite Error Database is locked – Error Code 5”. Before describing the solution for this problem we will first understand why this error message occurs in SQLite database.


Reasons for the SQLite Error Database is Locked

The error normally occurs when SQLite user attempts to accomplish two inappropriate operations on SQLite Database with using the same details with having same database connection. Below we have described the different situations in which there are chances of getting this Error:

1). If you have activated the SELECT operation on the Table and then you use the same table to perform write operation.

2). If you try to run DROP or CREATE command on the database while previous SELECT command is not yet completed.

The Select command in SQLite database is not considered to be completed until we run SQLite3_finalize () or SQLite3_reset () command and many of the SQLite users don’t know this. They run SELECT command receive the message SQLITE_DONE and things that previous command is finished. This is the main reason for the Error Code 5 i.e. SQLite Error Database is Locked.

How to Fix SQLite Error Database is Locked –Error Code 5

The easiest way to get rid of this error is to create a new database backup without having any locks in it. After creating the backup you have to replace the Original SQLIte DB with the created backup copy. Please follow the scrips given below to accomplish this task:

1). $Sqlite3 .x.Sqlite where .x.Sqlite is the name of the SQLite database

2). Sqlite> .backup main backup.Sqlite

3). Sqlite> .exit

After successfully executing the above script, confirm that both read and write operations are allowed in the database. Now you can delete the old DB file and you are good to go.

What we should so that SQLite Error Database is locked – Error Code 5 Does not occurs:

Reduce Concurrency by Rewriting the Code: As the name suggests it is the light database and when we try to do concurrent operations on the database we usually get errors. So it is recommended to reduce concurrency so that you would not get this type of errors.

Increase Database Default Timeout: If a transaction is taking longer time to perform some operation on the database then it is blocking other threads from accessing SQLite database. It may leads to Deadlock. To resolve this issue we should use Lock Timeout feature of SQLite. If it is found that a particular thread is waiting for the lock for more than the default configured time then it stops the process and we get SQLite DB locked error. We can increase the Database default time to get rid of this error.


In this article we have explained different scenarios of the SQLite Error Database Table is Locked along with the possible fixes. We hope that you were able to recover the database by using the steps mentioned above. If you have any question or suggestion regarding the information given above, please comment. We will try our best to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *