title: Failed to open Database in android app
body: <p>in my app i am trying to fetch some data from database and i am showing it in a list. When i run the app in my Motorola(2.1) and in Samsung Ace(2.3) and i some of my emulators they are working fine.</p>
  
  <p>But when i run in my Samsung  Galaxy Tab-P7500(3.1), it shows the following error</p>
  
  <pre><code> sqlite3_open_v2("/data/data/com.pack.store/databases/chaek.db", &amp;handle, 6, NULL) failed
      E/SQLiteDatabase(10021): Failed to open the database. closing it.
      E/SQLiteDatabase(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
      E/SQLiteDatabase(10021): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
      E/SQLiteDatabase(10021): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
      E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
      E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
      E/SQLiteDatabase(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
      E/SQLiteDatabase(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
      E/SQLiteDatabase(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
      E/SQLiteDatabase(10021): at android.os.Looper.loop(Looper.java:132)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
      E/SQLiteDatabase(10021): at java.lang.reflect.Method.invokeNative(Native Method)
      E/SQLiteDatabase(10021): at java.lang.reflect.Method.invoke(Method.java:491)
      E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
      E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
      E/SQLiteDatabase(10021): at dalvik.system.NativeStart.main(Native Method)
      E/SQLiteOpenHelper(10021): Couldn't open chaek.db for writing (will try read-only):
      E/SQLiteOpenHelper(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
      E/SQLiteOpenHelper(10021): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
      E/SQLiteOpenHelper(10021): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
      E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
      E/SQLiteOpenHelper(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
      E/SQLiteOpenHelper(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
      E/SQLiteOpenHelper(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
      E/SQLiteOpenHelper(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
      E/SQLiteOpenHelper(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
      E/SQLiteOpenHelper(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
      E/SQLiteOpenHelper(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
      E/SQLiteOpenHelper(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
      E/SQLiteOpenHelper(10021): at android.os.Looper.loop(Looper.java:132)
      E/SQLiteOpenHelper(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
      E/SQLiteOpenHelper(10021): at java.lang.reflect.Method.invokeNative(Native Method)
      E/SQLiteOpenHelper(10021): at java.lang.reflect.Method.invoke(Method.java:491)
      E/SQLiteOpenHelper(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
      E/SQLiteOpenHelper(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
      E/SQLiteOpenHelper(10021): at dalvik.system.NativeStart.main(Native Method)
      E/SqliteDatabaseCpp(10021): sqlite3_open_v2("/data/data/com.pack.sp/databases/chaek.db", &amp;handle, 1, NULL) failed
      E/SQLiteDatabase(10021): Failed to open the database. closing it.
      E/SQLiteDatabase(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
      E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:233)
      E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
      E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
      E/SQLiteDatabase(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
      E/SQLiteDatabase(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
      E/SQLiteDatabase(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
      E/SQLiteDatabase(10021): at android.os.Looper.loop(Looper.java:132)
      E/SQLiteDatabase(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
      E/SQLiteDatabase(10021): at java.lang.reflect.Method.invokeNative(Native Method)
      E/SQLiteDatabase(10021): at java.lang.reflect.Method.invoke(Method.java:491)
      E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
      E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
      E/SQLiteDatabase(10021): at dalvik.system.NativeStart.main(Native Method)
      E/android.os.Debug(  284): Dumpstate &gt; /data/log/dumpstate_app_error
  </code></pre>
  
  <p>Following is my Db class file</p>
  
  <pre><code>public class ChaekHelper extends SQLiteOpenHelper 
  {
      private static final int DATABASE_VERSION = 7;
      private static final String DATABASE_NAME = "chaek.db";
      private List&lt;Map&lt;String, String&gt;&gt; ALL_TABLES = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
      public ChaekHelper(Context context) 
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          this.onSetCreateQuery(context);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) 
      {
          for (int i = 0; i &lt; ALL_TABLES.size(); i++) 
          {
              db.execSQL("CREATE TABLE IF NOT EXISTS " + ALL_TABLES.get(i).get("TABLE_NAME") + " ( "+ ALL_TABLES.get(i).get("FIELD_NAME") + " );");
          }
  
          // create Index table
          db.execSQL("DROP TABLE IF EXISTS dual;");
          db.execSQL("CREATE TABLE dual (idx INTEGER PRIMARY KEY, rownum TEXT);");
          db.execSQL("CREATE UNIQUE INDEX idx_rownum ON dual (rownum);");
  
          // create all tables
          db.beginTransaction();
          try 
          {
              for (int i = 1; i &lt; 100; i++) {
                  String idxStr = "0" + i;
                  if (i &gt; 9)
                      idxStr = idxStr.substring(1);
  
                  db.execSQL("INSERT INTO dual (idx, rownum) VALUES (" + i + ",'" + idxStr + "');");
              }
              db.setTransactionSuccessful();
          }
          catch (Exception e) {
              e.printStackTrace();
          }
          finally {
              db.endTransaction();
          }
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          for (int i = 0; i &lt; ALL_TABLES.size(); i++) {
              db.execSQL("DROP TABLE IF EXISTS " + ALL_TABLES.get(i).get("TABLE_NAME") + ";");
          }
          onCreate(db);
      }
  
      private void onSetCreateQuery(Context context) {
          Map&lt;String, String&gt; params;
  
          // 1. BOOK
          params = new HashMap&lt;String, String&gt;();
          params.put("TABLE_NAME", context.getResources().getString(R.string.BOOK_TBL_NAME));
          params.put("FIELD_NAME", context.getResources().getString(R.string.BOOK_TBL_FIELDS));
          ALL_TABLES.add(params);
  
          // 2. TOC
          params = new HashMap&lt;String, String&gt;();
          params.put("TABLE_NAME", context.getResources().getString(R.string.TOC_TBL_NAME));
          params.put("FIELD_NAME", context.getResources().getString(R.string.TOC_TBL_FIELDS));
          ALL_TABLES.add(params);
  
          // 3. BOOKMARK
          params = new HashMap&lt;String, String&gt;();
          params.put("TABLE_NAME", context.getResources().getString(R.string.BM_TBL_NAME));
          params.put("FIELD_NAME", context.getResources().getString(R.string.BM_TBL_FIELDS));
          ALL_TABLES.add(params);
      }
  }
  </code></pre>
  
  <p>Why it happens like this? can any one explain me about this.... i had wasted three days in this....</p>