title: Database is locked - how to fix?
body: <p>I set <code>fireBase</code> in my project and in <code>crashReporting</code> show bellow errors :</p>
  
  <pre><code>Exception java.lang.Throwable: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode ################################################################# Error Code : 5 (SQLITE_BUSY) Caused By : The database file is locked. (database is locked (code 5): , while compiling: PRAGMA journal_mode) #################################################################
  ....data.source.local.DataBaseInstagram.insertToProductsDrafts (DataBaseInstagram.java:132)
  </code></pre>
  
  <p>And :</p>
  
  <pre><code>Exception java.lang.Throwable: android.database.sqlite.SQLiteException: no such table: ProductsDrafts (code 1): , while compiling: select count(*) from ProductsDrafts ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such table: ProductsDrafts (code 1): , while compiling: select count(*) from ProductsDrafts) #################################################################
  ....data.source.local.DataBaseInstagram.insertToProductsDrafts (DataBaseInstagram.java:132)
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>Exception java.lang.Throwable: android.database.sqlite.SQLiteException: no such table: ProductsDrafts (code 1): , while compiling: select * from ProductsDrafts ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such table: ProductsDrafts (code 1): , while compiling: select * from ProductsDrafts) #################################################################
  ....data.source.local.DataBaseInstagram.selectFromProductsDraftsTable (DataBaseInstagram.java:283)
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>Exception java.lang.Throwable: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5) ################################################################# Error Code : 5 (SQLITE_BUSY) Caused By : The database file is locked. (database is locked (code 5)) #################################################################
  ....data.source.local.DataBaseInstagram.selectFromProductsDraftsTable (DataBaseInstagram.java:283)
  </code></pre>
  
  <p>And bellow is my codes :</p>
  
  <pre><code>   public class DataBaseInstagram extends SQLiteOpenHelper {
  
      private static final int DATABASE_VERSION = 7;
      private static final String DATABASE_NAME = "Instagram";
  
  
      //region Instagram table
      private static final String TABLE_PRODUCTS = "DetailImage";
      private static final String COLUMN_IMAGE_NAME = "imageName";
      private static final String COLUMN_IMAGE_ID = "imageID";
      //endregion
  
      //region Products drafts
      private static final String TABLE_PRODUCTS_DRAFTS = "ProductsDrafts";
      private static final String COLUMN_PRODUCT_NAME = "productName";
      private static final String COLUMN_PRODUCT_PRICE = "productPrice";
      private static final String COLUMN_PRODUCT_PREPARATION_TIME = "ProductPreparationTime";
      private static final String COLUMN_PRODUCT_QUANTITY = "ProductQuantity";
      private static final String COLUMN_PRODUCT_BRIEF_DESCRIPTION = "ProductBriefDescription";
      private static final String COLUMN_PRODUCT_DESCRIPTION = "ProductDescription";
      private static final String COLUMN_PRODUCT_SUB_CATEGORY_ID = "subCategoryId";
      private static final String COLUMN_PRODUCT_SUB_CATEGORY_TITLE = "subCategoryTitle";
      private static final String COLUMN_PRODUCT_PROPERTIES = "productProperties";
      private static final String COLUMN_PRODUCT_KEYWORDS = "Keywords";
      private static final String COLUMN_PRODUCT_PUBLISH_PRODUCT = "PublishProduct";
      private static final String COLUMN_PRODUCT_STATUS = "ProductStatus";
      private static final String COLUMN_PRODUCT_STATUS_ACTION_USER = "StatusActionUser";
      //endregion
  
      public DataBaseInstagram(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_PRODUCTS_TABLE =
                  "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                          COLUMN_IMAGE_NAME + " TEXT," + COLUMN_IMAGE_ID + " TEXT" + ")";
          String CREATE_PRODUCTS_DRAFTS_TABLE =
                  "CREATE TABLE " + TABLE_PRODUCTS_DRAFTS + "(" +
                          COLUMN_PRODUCT_NAME + " TEXT," +
                          COLUMN_PRODUCT_PRICE + " TEXT," +
                          COLUMN_PRODUCT_PREPARATION_TIME + " TEXT," +
                          COLUMN_PRODUCT_QUANTITY + " TEXT," +
                          COLUMN_PRODUCT_BRIEF_DESCRIPTION + " TEXT," +
                          COLUMN_PRODUCT_DESCRIPTION + " TEXT," +
                          COLUMN_PRODUCT_SUB_CATEGORY_ID + " TEXT," +
                          COLUMN_PRODUCT_SUB_CATEGORY_TITLE + " TEXT," +
                          COLUMN_PRODUCT_PROPERTIES + " TEXT," +
                          COLUMN_PRODUCT_KEYWORDS + " TEXT," +
                          COLUMN_PRODUCT_PUBLISH_PRODUCT + " TEXT," +
                          COLUMN_PRODUCT_STATUS + " TEXT," +
                          COLUMN_PRODUCT_STATUS_ACTION_USER + " INTEGER" + ")";
          db.execSQL(CREATE_PRODUCTS_TABLE);
          db.execSQL(CREATE_PRODUCTS_DRAFTS_TABLE);
          String INDEX = "CREATE UNIQUE INDEX products_index ON "
                  + TABLE_PRODUCTS + " (imageID)";
          db.execSQL(INDEX);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          if (newVersion &gt; oldVersion) {
              db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS_DRAFTS);
              db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
              onCreate(db);
          }
      }
  
      public Long insertToProductsDrafts(String productName,
                                         String productPrice,
                                         String productPreparationTime,
                                         String productQuantity,
                                         String productBriefDescription,
                                         String productDescription,
                                         String subCategoryId,
                                         String subCategoryTitle,
                                         String properties,
                                         String keywords,
                                         String publishProduct,
                                         String productStatus,
                                         Integer statusActionUser) {
          Log.i("SDASDADASDDS"," - " );
          Long id = null;
          SQLiteDatabase db = null;
          try {
              db = this.getWritableDatabase();
              db.beginTransactionNonExclusive();
              ContentValues values = new ContentValues();
              int NoOfRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS_DRAFTS);
              if (NoOfRows &gt; 0) {
                  db.execSQL("DELETE FROM " + TABLE_PRODUCTS_DRAFTS);
              }
              values.put(COLUMN_PRODUCT_NAME, productName);
              values.put(COLUMN_PRODUCT_PRICE, productPrice);
              values.put(COLUMN_PRODUCT_PREPARATION_TIME, productPreparationTime);
              values.put(COLUMN_PRODUCT_QUANTITY, productQuantity);
              values.put(COLUMN_PRODUCT_BRIEF_DESCRIPTION, productBriefDescription);
              values.put(COLUMN_PRODUCT_DESCRIPTION, productDescription);
              values.put(COLUMN_PRODUCT_SUB_CATEGORY_ID, subCategoryId);
              values.put(COLUMN_PRODUCT_SUB_CATEGORY_TITLE, subCategoryTitle);
              values.put(COLUMN_PRODUCT_PROPERTIES, properties);
              values.put(COLUMN_PRODUCT_KEYWORDS, keywords);
              values.put(COLUMN_PRODUCT_PUBLISH_PRODUCT, publishProduct);
              values.put(COLUMN_PRODUCT_STATUS, productStatus);
              values.put(COLUMN_PRODUCT_STATUS_ACTION_USER, statusActionUser);
              id = db.insert(TABLE_PRODUCTS_DRAFTS, null, values);
          } catch (Exception ex) {
              Throwable t = new Throwable(ex).fillInStackTrace();
              FirebaseCrash.report(t);
              if (db != null) {
                  db.close();
              }
          } finally {
              if (db != null) {
                  db.setTransactionSuccessful();
                  db.endTransaction();
                  db.close();
              }
          }
          return id;
      }
  
      public void deleteFromProductsDraftsTable() {
          SQLiteDatabase db = null;
          try {
              db = this.getWritableDatabase();
              int NoOfRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS_DRAFTS);
              if (NoOfRows &gt; 0) {
                  db.execSQL("DELETE FROM " + TABLE_PRODUCTS_DRAFTS);
              }
              db.close();
          } catch (Exception ex) {
              Throwable t = new Throwable(ex).fillInStackTrace();
              FirebaseCrash.report(t);
              if (db != null) {
                  db.close();
              }
          }
      }
  
      public Product selectFromProductsDraftsTable() {
          Product product = null;
          SQLiteDatabase db = null;
          try {
              product = null;
              Gson gson = new Gson();
              Type listType = new TypeToken&lt;List&lt;Product.ProductProperty&gt;&gt;() {
              }.getType();
              String sql = "select * from " + TABLE_PRODUCTS_DRAFTS;
              db = this.getReadableDatabase();
              Cursor cursor = db.rawQuery(sql, null);
              if (cursor.moveToFirst()) {
                  do {
                      List&lt;Product.ProductProperty&gt; lsProductProperty = new ArrayList&lt;&gt;();
                      String productPropertyText = null;
                      List&lt;Product.ProductProperty&gt; propertyList = null;
                      if (cursor.getString(8) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(8))) {
                              productPropertyText = cursor.getString(8);
                          }
                      }
                      if (productPropertyText != null) {
                          if (!TextUtils.isEmpty(productPropertyText)) {
                              propertyList = gson.fromJson(productPropertyText, listType);
                          }
                      }
                      if (productPropertyText != null) {
                          if (!TextUtils.isEmpty(productPropertyText)) {
                              if (propertyList != null) {
                                  if (!propertyList.isEmpty()) {
                                      for (int i = 0; i &lt; propertyList.size(); i++) {
                                          Product.ProductProperty productProperty = new Product.ProductProperty(
                                                  propertyList.get(i).getId(),
                                                  propertyList.get(i).getTitle(),
                                                  propertyList.get(i).getMeasurementUnit(),
                                                  propertyList.get(i).getValue());
                                          lsProductProperty.add(productProperty);
                                      }
                                  }
                              }
                          }
                      }
                      Boolean published = null;
                      if (cursor.getString(10) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(10))) {
                              published = Boolean.parseBoolean(cursor.getString(10));
                          }
                      }
                      Integer preparationTime = null;
                      if (cursor.getString(2) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(2))) {
                              preparationTime = Integer.parseInt(cursor.getString(2));
                          }
                      }
                      String keywords = null;
                      if (cursor.getString(9) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(9))) {
                              keywords = cursor.getString(9);
                          }
                      }
                      Integer quantity = null;
                      if (cursor.getString(3) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(3))) {
                              quantity = Integer.parseInt(cursor.getString(3));
                          }
                      }
                      Integer status = null;
                      if (cursor.getString(11) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(11))) {
                              status = Integer.parseInt(cursor.getString(11));
                          }
                      }
                      Product.ExtraInfo extraInfo = new Product.ExtraInfo(lsProductProperty,
                              published,
                              preparationTime,
                              keywords,
                              quantity,
                              status);
                      String name = null;
                      if (cursor.getString(0) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(0))) {
                              name = cursor.getString(0);
                          }
                      }
                      String price = null;
                      if (cursor.getString(1) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(1))) {
                              price = cursor.getString(1);
                          }
                      }
                      String briefDescription = null;
                      if (cursor.getString(4) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(4))) {
                              briefDescription = cursor.getString(4);
                          }
                      }
                      String description = null;
                      if (cursor.getString(5) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(5))) {
                              description = cursor.getString(5);
                          }
                      }
                      int subCategoryId = 0;
                      if (cursor.getInt(6) &gt; 0) {
                          subCategoryId = cursor.getInt(6);
                      }
                      String categoryTitle = null;
                      if (cursor.getString(7) != null) {
                          if (!TextUtils.isEmpty(cursor.getString(7))) {
                              categoryTitle = cursor.getString(7);
                          }
                      }
                      product = new Product(name,
                              price,
                              briefDescription,
                              description,
                              subCategoryId,
                              categoryTitle,
                              extraInfo);
                  } while (cursor.moveToNext());
              }
              cursor.close();
              db.close();
          } catch (Exception ex) {
              Throwable t = new Throwable(ex).fillInStackTrace();
              FirebaseCrash.report(t);
              if (db != null) {
                  db.close();
              }
          }
          return product;
      }
  
      public Long insertToInstagramTable(String imageID, String imageName) {
          Long status = null;
          SQLiteDatabase db = null;
          try {
              ContentValues values = new ContentValues();
              db = this.getWritableDatabase();
              values.put(COLUMN_IMAGE_ID, imageID);
              values.put(COLUMN_IMAGE_NAME, imageName);
              status = db.insert(TABLE_PRODUCTS, null, values);
              db.close();
          } catch (Exception ex) {
              Throwable t = new Throwable(ex).fillInStackTrace();
              FirebaseCrash.report(t);
              if (db != null) {
                  db.close();
              }
          }
          return status;
      }
  
      public List&lt;ExistImage&gt; selectFromInstagramTable() {
          List&lt;ExistImage&gt; lstExistImages = null;
          SQLiteDatabase db = null;
          try {
              String sql = "select * from " + TABLE_PRODUCTS;
              db = this.getReadableDatabase();
              lstExistImages = new ArrayList&lt;&gt;();
              Cursor cursor = db.rawQuery(sql, null);
              if (cursor.moveToFirst()) {
                  do {
                      ExistImage existImage = new ExistImage();
                      existImage.setImageName(cursor.getString(0));
                      existImage.setImageID(cursor.getString(1));
                      lstExistImages.add(existImage);
                  } while (cursor.moveToNext());
              }
              cursor.close();
              db.close();
          } catch (Exception ex) {
              Throwable t = new Throwable(ex).fillInStackTrace();
              FirebaseCrash.report(t);
              if (db != null) {
                  db.close();
              }
          }
          return lstExistImages;
      }
  }
  </code></pre>