2

I set fireBase in my project and in crashReporting show bellow errors :

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)

And :

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)

And:

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)

And:

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)

And bellow is my codes :

   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 > 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 > 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 > 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<List<Product.ProductProperty>>() {
            }.getType();
            String sql = "select * from " + TABLE_PRODUCTS_DRAFTS;
            db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                do {
                    List<Product.ProductProperty> lsProductProperty = new ArrayList<>();
                    String productPropertyText = null;
                    List<Product.ProductProperty> 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 < 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) > 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<ExistImage> selectFromInstagramTable() {
        List<ExistImage> lstExistImages = null;
        SQLiteDatabase db = null;
        try {
            String sql = "select * from " + TABLE_PRODUCTS;
            db = this.getReadableDatabase();
            lstExistImages = new ArrayList<>();
            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;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

SQLiteException

A SQLite exception that indicates there was an error with SQL parsing or execution.

Exception java.lang.Throwable: android.database.sqlite.SQLiteException: no such table: ProductsDrafts (code 1): , while compiling: select * from ProductsDrafts

Make sure ProductsDrafts is Present in Database.

FYI

If you add table after publish, then add onUpgrade()

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
if(newVersion>oldVersion)

 // ALTAR TABLE STATEMENT
 // DROP TABLE STATEMENT 

}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • @JoJoRoid You can read https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – IntelliJ Amiya Jan 11 '18 at 08:05
  • @ IntelliJ Amiya. I update my question and edit onUpgrade() .Please see .Thanks a lot –  Jan 11 '18 at 08:13
  • @JoJoRoid set `private static final String DATABASE_NAME = "Instagram.db";` – IntelliJ Amiya Jan 11 '18 at 08:14
  • 1
    @ IntelliJ Amiya. This code is true ? @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion > oldVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS_DRAFTS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS); onCreate(db); } } –  Jan 11 '18 at 08:17