0

I am making an ecommerce app. In this app I have made a table in which I am storing the following details of products:

  • three images of the product;
  • name;
  • price;
  • description;
  • the unit of the material of product (e.g. kg, litre, gram etc.).

Adding the product works fine but I am facing a problem in retrieving data. The exact problem is that whenever I try to retrieve the whole data from the table using query SELECT * FROM productsBox it is neither returning me anything nor giving me any error just returning zero rows from the database. I have tried to retrieve data by writing all column names manually instead of * but faced the same problem. And the most important point is that this is happening only when I am trying to get the "image3" column. I don't know why, but this column causes a problem.

Database class

public class AddProductDatabase extends SQLiteOpenHelper {
    public AddProductDatabase(@Nullable Context context) {
        super(context, "products.db", null, 9);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableStatement = "CREATE TABLE products (id INTEGER PRIMARY KEY AUTOINCREMENT , image1 BLOB , image2 BLOB , image3 BLOB , productName TEXT ,productPrice INTEGER , unit TEXT, productDescription TEXT) ";
        db.execSQL(createTableStatement);
    }

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

      /* String createTableStatement = "CREATE TABLE category (id INTEGER PRIMARY KEY AUTOINCREMENT , image1 BLOB,catName TEXT) ";
        db.execSQL(createTableStatement);
        String createTableStatement2 = "CREATE TABLE productsBox (id INTEGER PRIMARY KEY AUTOINCREMENT , image1 BLOB , image2 BLOB , image3 BLOB , productName TEXT ,productPrice INTEGER , unit TEXT, productDescription TEXT) ";
        db.execSQL(createTableStatement2);*/
      //  String createTableStatement2 = "CREATE TABLE profile (id INTEGER PRIMARY KEY AUTOINCREMENT , profileImage BLOB ,name TEXT ,phone INTEGER ,address TEXT,pincode INTEGER,role INTEGER,passw TEXT) ";
       // db.execSQL(createTableStatement2);
        String createTableStatement2 = "CREATE TABLE UserProfile (id INTEGER PRIMARY KEY AUTOINCREMENT , profileImage BLOB ,name TEXT ,phone TEXT ,address TEXT,pincode INTEGER,role INTEGER,passw TEXT) ";
        db.execSQL(createTableStatement2);
    }

    public boolean addProduct(ArrayList<byte[]> img, String name, int price, String unit, String description) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("image1", img.get(0));
        cv.put("image2", img.get(1));
        cv.put("image3", img.get(2));
        cv.put("productName", name);
        cv.put("productPrice", price);
        cv.put("productDescription", description);
        cv.put("unit", unit);
        long products = db.insert("productsBox", null, cv);
        db.close();
        if (products == -1) {
            return false;
        } else {
            return true;
        }

    }


    public ArrayList<productmodal> getAll() {
        ArrayList<productmodal> array = new ArrayList<>();
        String querySelect = "SELECT id,image1,image2,productName,productPrice,productDescription,unit FROM productsBox";
        SQLiteDatabase dbb = this.getReadableDatabase();
        Cursor cursorSelect = dbb.rawQuery(querySelect, null);
       // DatabaseUtils.dumpCursor(cursorSelect);
        int ind_image = cursorSelect.getColumnIndex("image1");
        int ind_productName = cursorSelect.getColumnIndex("productName");
        int ind_productPrice = cursorSelect.getColumnIndex("productPrice");
        int ind_id = cursorSelect.getColumnIndex("id");
        int ind_desc = cursorSelect.getColumnIndex("productDescription");
        int ind_unit = cursorSelect.getColumnIndex("unit");
        int ind_img3 = cursorSelect.getColumnIndex("image2");


        if (cursorSelect.moveToFirst()) {
            do {
                byte[] image = cursorSelect.getBlob(ind_image);
                String name = cursorSelect.getString(ind_productName);
                int price = cursorSelect.getInt(ind_productPrice);
                int id = cursorSelect.getInt(ind_id);
                productmodal model = new productmodal(image, name, price,id);
                array.add(model);
            } while (cursorSelect.moveToNext());
        } else {

        }
        cursorSelect.close();
        dbb.close();
        return array;
    }

    public ArrayList<productDetailModal> getProduct(int idd) {
        ArrayList<productDetailModal> array = new ArrayList<>();
        String querySelect = "SELECT id,image1,image2,image3,productName,productPrice,productDescription,unit FROM productsBox WHERE id ="+idd;
        SQLiteDatabase dbb = this.getReadableDatabase();
        Cursor cursorSelect = dbb.rawQuery(querySelect, null);
        DatabaseUtils.dumpCursor(cursorSelect);
        int ind_unit = cursorSelect.getColumnIndex("unit");
        int ind_productName = cursorSelect.getColumnIndex("productName");
        int ind_productPrice = cursorSelect.getColumnIndex("productPrice");
        int ind_id = cursorSelect.getColumnIndex("id");
        int ind_img1 = cursorSelect.getColumnIndex("image1");
        int ind_img2 = cursorSelect.getColumnIndex("image2");
        int ind_img3 = cursorSelect.getColumnIndex("image3");
        int ind_desc = cursorSelect.getColumnIndex("productDescription");

        if (cursorSelect.moveToFirst()) {
            do {
                String name = cursorSelect.getString(ind_productName);
                int price = cursorSelect.getInt(ind_productPrice);
                int id = cursorSelect.getInt(ind_id);
                byte[] proimg1= cursorSelect.getBlob(ind_img1);
                byte[] proimg2= cursorSelect.getBlob(ind_img2);
                byte[] proimg3= cursorSelect.getBlob(ind_img3);
                String desc = cursorSelect.getString(ind_desc);
                String unit = cursorSelect.getString(ind_unit);

                productDetailModal model = new productDetailModal(id,price,name,desc,unit ,proimg1,proimg2,proimg3);
                array.add(model);
            } while (cursorSelect.moveToNext());
        } else {

        }
        cursorSelect.close();
        dbb.close();
        return array;
    }

}

here getALL() method is working fine as I have not added the image3 column but getProduct() is returning nothing as here I have added the image3 column.

productDetailModal class

public class productDetailModal {
    int idd, price;
    String productname, productdesc,productunit;
    byte[] img1,img2,img3;

    public productDetailModal(int idd, int price, String productname, String productdesc, String productunit, byte[] img1, byte[] img2, byte[] img3) {
        this.idd = idd;
        this.price = price;
        this.productname = productname;
        this.productdesc = productdesc;
        this.productunit = productunit;
        this.img1 = img1;
        this.img2 = img2;
        this.img3 = img3;
    }

    public int getIdd() {
        return idd;
    }

    public void setIdd(int idd) {
        this.idd = idd;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getProductname() {
        return productname;
    }

    public void setProductname(String productname) {
        this.productname = productname;
    }

    public String getProductdesc() {
        return productdesc;
    }

    public void setProductdesc(String productdesc) {
        this.productdesc = productdesc;
    }

    public String getProductunit() {
        return productunit;
    }

    public void setProductunit(String productunit) {
        this.productunit = productunit;
    }

    public byte[] getImg1() {
        return img1;
    }

    public void setImg1(byte[] img1) {
        this.img1 = img1;
    }

    public byte[] getImg2() {
        return img2;
    }

    public void setImg2(byte[] img2) {
        this.img2 = img2;
    }

    public byte[] getImg3() {
        return img3;
    }

    public void setImg3(byte[] img3) {
        this.img3 = img3;
    }
}

productModal class

public class productmodal {

    byte[] productImage;
    String productName;
    int productPrice , id ;

    public productmodal(byte[] productImage, String productName, int productPrice, int id) {
        this.productImage = productImage;
        this.productName = productName;
        this.productPrice = productPrice;
        this.id = id;
    }

    public byte[] getProductImage() {
        return productImage;
    }

    public void setProductImage(byte[] productImage) {
        this.productImage = productImage;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(int productPrice) {
        this.productPrice = productPrice;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Could it be that the size of the data is a problem? Like "retrieving data of bigger size from database SQLite not allowed"-type rule or something ?

Aron Hoogeveen
  • 437
  • 5
  • 16

1 Answers1

0

Could it be that the size of the data is a problem? Like "retrieving data of bigger size from database SQLite not allowed"-type rule or something ?

It could be and is most likely the issue. There is a limit on the size of a CursorWindow it being up to 4Mb, if a row will not fit into the CursorWindow then even though the data exists in the database it cannot be extracted as a single row.

As such it's not an SQLite restriction but a restriction of the Android API.

The recommended fix is to not store images in the database but to instead store the image as a file and to store the path to the file (or a part of the path that is enough to determine where to retrieve the image from).

You may wish to consider the following links that delve into this area and provide potential solutions :-

MikeT
  • 51,415
  • 16
  • 49
  • 68