【发布时间】:2022-01-20 11:11:55
【问题描述】:
我正在制作一个电子商务应用程序。在这个应用程序中,我制作了一个表格,其中存储了产品的以下详细信息:
- 产品的三张图片;
- 姓名;
- 价格;
- 说明;
- 产品材料的单位(如公斤、升、克等)。
添加产品效果很好,但我在检索数据时遇到了问题。
确切的问题是,每当我尝试使用查询SELECT * FROM productsBox 从表中检索整个数据时,它既没有返回任何内容,也没有给我任何错误,只是从数据库返回零行。我试图通过手动编写所有列名而不是 * 来检索数据,但遇到了同样的问题。
最重要的一点是,只有当我试图获取“image3”列时才会发生这种情况。我不知道为什么,但是这个专栏有问题。
数据库类
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;
}
}
这里 getALL() 方法工作正常,因为我没有添加 image3 列,但 getProduct() 没有返回任何内容,因为这里我添加了 image3 列。
productDetailModal类
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类
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;
}
}
会不会是数据的大小有问题?像“不允许从数据库 SQLite 中检索更大尺寸的数据”类型的规则之类的?
【问题讨论】:
标签: java android database sqlite android-sqlite