{"id":5723,"date":"2014-04-06T10:29:36","date_gmt":"2014-04-06T10:29:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/sqlite-exception-database-is-locked-issue-collection-of-common-programming-errors\/"},"modified":"2014-04-06T10:29:36","modified_gmt":"2014-04-06T10:29:36","slug":"sqlite-exception-database-is-locked-issue-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/sqlite-exception-database-is-locked-issue-collection-of-common-programming-errors\/","title":{"rendered":"SQLite exception: Database is locked issue-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m having an issue with my SQLite database inside an Android app. It seems to happen every so often and I cannot reproduce it, but this is the report the Android Market gives.<\/p>\n<p>Basically I have first a splash screen activity that starts by checking if all the needed data is in the database. If not, it will install the data in a asynchronous thread and close the db connection.<\/p>\n<p>Then and only then can the main activity be started, which also opens and reads\/writes the database.<\/p>\n<p>This code is executed from the onCreate method of the splash screen activity:<\/p>\n<pre><code>dbWord = new WordDBAdapter(this, myUI);\ndbWord.open();\nif (dbWord.isDataInstalled()) {\n    dbWord.close();\n    databaseInstalled = true;\n    clickToStartView.setText(myUI.PRESS_TO_START);\n    clickToStartView.startAnimation(myBlinkAnim);\n} else {\n    \/\/ If not, try to install in asynchronous thread\n    progressDialog = new ProgressDialog(this);\n    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\n    progressDialog.setMessage(myUI.INSTALLING_DB);\n    progressDialog.setCancelable(false);\n    new InstallDBData().execute(\"\");\n}\n<\/code><\/pre>\n<p>The code for the asynchronous thread is:<\/p>\n<pre><code>private class InstallDBData extends AsyncTask {\n\n    @Override\n    protected Integer doInBackground(String... arg0) {\n        progressDialog.setProgress(0);\n        dbWord.installData(0, getApplicationContext());\n        progressDialog.setProgress(20);\n        dbWord.installData(1, getApplicationContext());\n        progressDialog.setProgress(40);\n        dbWord.installData(2, getApplicationContext());\n        progressDialog.setProgress(60);\n        dbWord.installData(3, getApplicationContext());\n        progressDialog.setProgress(80);\n        dbWord.installData(4, getApplicationContext());\n        progressDialog.setProgress(100);\n        return 1;\n    }\n\n    protected void onPreExecute() {\n        progressDialog.show();\n    }\n\n    protected void onPostExecute(Integer x) {\n        dbWord.close();\n        progressDialog.hide();\n        databaseInstalled = true;\n        clickToStartView.setText(myUI.PRESS_TO_START);\n        clickToStartView.startAnimation(myBlinkAnim);\n    }\n}\n<\/code><\/pre>\n<p>These are important parts of the WordDBAdapter class, which is also used by the main activity:<\/p>\n<pre><code>public class WordDBAdapter {\n    private DatabaseHelper mDbHelper;\n    private SQLiteDatabase mDb;\n\n    public WordDBAdapter open() throws android.database.SQLException {\n        mDbHelper = new DatabaseHelper(mCtx);\n        mDb = mDbHelper.getWritableDatabase();\n        return this;\n    }\n\n    public void close() {\n        mDbHelper.close();\n    }\n    ...\n}\n<\/code><\/pre>\n<p>I&#8217;m getting the following exceptions, which are similar but have a different message:<\/p>\n<p>First type of error message:<\/p>\n<pre><code>java.lang.RuntimeException: Unable to start activity \n  ComponentInfo{example.flashcards.medical\/com.example.flashcards.common.SplashWindow}: \n  android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;\nat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)\n....\nCaused by: android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;\nat android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)\nat android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1870)\nat android.database.sqlite.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:602)\n<\/code><\/pre>\n<p>Second type of error message:<\/p>\n<pre><code>java.lang.RuntimeException: Unable to start activity \n  ComponentInfo{example.flashcards.medical\/com.example.flashcards.common.SplashWindow}: \n  android.database.sqlite.SQLiteException: database is locked\nat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)\nat android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)\n....\nCaused by: android.database.sqlite.SQLiteException: database is locked\nat android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)\nat android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1987)\nat android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1855)\nat android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)\nat android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)\n<\/code><\/pre>\n<p>I would really like to not have to create a ContentProvider, because I believe its overkill if a simpler solution is available. Also, only this application has access to the database.<\/p>\n<p>Any suggestions on how this can be fixed?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m having an issue with my SQLite database inside an Android app. It seems to happen every so often and I cannot reproduce it, but this is the report the Android Market gives. Basically I have first a splash screen activity that starts by checking if all the needed data is in the database. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5723","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5723"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5723\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}