{"id":255,"date":"2016-01-19T10:59:11","date_gmt":"2016-01-19T10:59:11","guid":{"rendered":"https:\/\/www.acquireforensics.com\/blog\/?p=255"},"modified":"2016-01-27T08:17:35","modified_gmt":"2016-01-27T08:17:35","slug":"sqlite-database-is-locked-error-code-5","status":"publish","type":"post","link":"https:\/\/www.acquireforensics.com\/blog\/sqlite-database-is-locked-error-code-5.html","title":{"rendered":"How To Remove Sqlite Database is Locked Error Code 5"},"content":{"rendered":"<p>At times while performing transactions in Sqlite database, the users might encounter error where the database gets locked and the following error occurs:<\/p>\n<p><strong>\u201cError Code 5 Database is locked\u201d<\/strong><\/p>\n<h2><strong>Reason Behind The Error <\/strong><\/h2>\n<p>This error code is generated when the user tries to perform two incompatible operations on a database at the same instance and on the same database connection. The <strong>SQLITE_LOCKED<\/strong> code indicates that an operation could not be continued because of a conflict with a transaction that uses the same database connection or the transaction that uses a different database connection using a shared cache.<\/p>\n<p>An example of this is when the user tries to run a DROP TABLE statement while a different thread is trying to read from the same table and that too on the same database connection. This is because when the <strong>DROP TABLE<\/strong> command is run. The table would be deleted and therefore, the other thread will not be able to read from it.<\/p>\n<h2><strong>Scenarios of Sqlite Database Is Locked Error Code 5<\/strong><\/h2>\n<p>There are many scenarios when user received error code 5 database is locked. Some of them are:<\/p>\n<ul>\n<li>When a user is in the process to CREATE or DROP an index or a table whilst the SELECT statement is in a pending state, the database gets locked. This is because the users think that the SELECT statement has been finished as Sqlite3_() has returned the value SQLITE_DONE. However, this is not the case since SELECT statement is not considered as complete until <strong>Sqlite3_reset()<\/strong> or <strong>Sqlite3_finalize()<\/strong> are called.<\/li>\n<\/ul>\n<ul>\n<li>When a user tries to write in a table on which the SELECT operation is still active.<\/li>\n<\/ul>\n<ul>\n<li>When the user tries to do 2 SELECT on the same table and at the same instant in a multithread application and the Sqlite has not been set to do the same, the database may get locked.<\/li>\n<\/ul>\n<p>It is to be noted that SQLITE_LOCKED should not be confused with SQLITE_BUSY parameter. This is because SQLITE_LOCKED indicates to a condition when there is a conflict between two transaction running on the same database connection and the SQLITE_BUSY indicates that two transactions running on different database connection and in different processes have conflicted.<\/p>\n<h3><strong>Resolution to Remove Sqlite Locked Error<\/strong><\/h3>\n<p>In order to remove \u201cSqlite database is locked error code 5\u201d the most comprehensive solution is to create a backup of the database, which will not have any locks on it and then replacing the database with its backup copy. Follow the below-mentioned script to do the same where .x.Sqlite is the Sqlite database file:<\/p>\n<p><strong>$Sqlite3 .x.Sqlite<\/strong><\/p>\n<p><strong>Sqlite&gt; .backup main backup.Sqlite<\/strong><\/p>\n<p><strong>Sqlite&gt; .exit<\/strong><\/p>\n<p>Now the next step is to have a file named backup.Sqlite in the same directory. Then swap your old database with the backup copy of the database. Since the backup copy will not have any locks, the Sqlite database is locked error code 5 will not be encountered.<\/p>\n<p><strong>$mv .x.Sqlite old.Sqlite<\/strong><\/p>\n<p><strong>$mv backup.Sqlite .x.Sqlite<\/strong><\/p>\n<p>Once the above script is executed successfully, the users can again access the Sqlite database. Once you are sure that the database is allowing both the read and write operations to run successfully, you can delete the old <strong><a href=\"https:\/\/www.acquireforensics.com\/blog\/sqlite-database-structure.html\">Sqlite database file<\/a><\/strong>. By above mentioned process user can remove \u201cerror code 5 database is locked\u201d error without any hassle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At times while performing transactions in Sqlite database, the users might encounter error where the database gets locked and the following error occurs: \u201cError Code 5 Database is locked\u201d Reason Behind The Error This error code is generated when the user tries to perform two incompatible operations on a database at the same instance and [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":256,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-file-format"],"_links":{"self":[{"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":2,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions\/259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.acquireforensics.com\/blog\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}