由于id列,未能获得刚添加的行

时间:2019-04-21 06:24:37

标签: android

我有一个带有字段int ID和其他一些字段的类产品。我创建的表产品比MainActivity中的要多,我使用循环将38个项目自动插入该表中。我可以从表中获得38个项目的清单。

然后,我创建一个具有输入名称等的新活动。从Editext开始,我调用插入方法并将其传递给方法,但我仍然使用相同的插入方法。我可以毫无问题地得到39件物品的清单,

但是在添加项目40后,我的应用程序崩溃了,并且出现错误“无法从游标窗口读取行0,列0”,它指向我要获取列ID的行。我不知道为什么,因为我用相同的方法成功地获得了包含39个项目的整个列表,所以我认为问题可能是列ID没有增加。请帮助

  public ArrayList<ProductsPainting> getProductsList() {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<ProductsPainting> productsList = new ArrayList<>();
        String queryGetFabricPainting = "Select " + DatabaseContract.TABLE_NAME_PRODUCTS + ".*,"+ DatabaseContract.TABLE_NAME_MATERIAL + "." + DatabaseContract.Table_Material.COLUMN_NAME_MATERIAL + " from " + DatabaseContract.TABLE_NAME_PRODUCTS + " INNER JOIN " + DatabaseContract.TABLE_NAME_MATERIAL + " ON " + DatabaseContract.TABLE_NAME_PRODUCTS + "." + DatabaseContract.COLUMN_NAME_MATERIALID + " = " + DatabaseContract.TABLE_NAME_MATERIAL + "." + DatabaseContract.COLUMN_NAME_MATERIALID;
        Cursor cursor = db.rawQuery(queryGetFabricPainting, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    // here I seperate this and the error line takes me to this line
                    int id = (int) cursor.getLong(cursor.getColumnIndex(DatabaseContract.COLUMN_NAME_PRODUCTSID));
                    productsList.add(new ProductsPainting(id,
                            cursor.getString(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_DESCRIPTION)),
                            cursor.getString(cursor.getColumnIndex(DatabaseContract.Table_Material.COLUMN_NAME_MATERIAL)),
                            cursor.getDouble(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_PRICE)),
                            cursor.getBlob(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_IMAGE)),
                            cursor.getString(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_SIZE)),
                            cursor.getInt(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_QUANTITY)),
                            cursor.getInt(cursor.getColumnIndex(DatabaseContract.Table_Products.COLUMN_NAME_FAVORITE))));
                } while (cursor.moveToNext());

            }
            cursor.close();
        }
        db.close();
        return new ArrayList<>(productsList);
    }
  public void insertTableProducts(String description, long material,
                                    Double price, String size, byte[] image, int quantity, int favorite) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_DESCRIPTION, description);
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_PRICE, price);
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_SIZE, size);
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_IMAGE, image);
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_FAVORITE, favorite);
        values.put(DatabaseContract.COLUMN_NAME_MATERIALID, material);
        values.put(DatabaseContract.Table_Products.COLUMN_NAME_QUANTITY, quantity);
        values.put(DatabaseContract.COLUMN_NAME_CARTID, 0);
        db.insert(DatabaseContract.TABLE_NAME_PRODUCTS, null, values);
        db.close();
    }

这是我的创建表产品

public static final String COLUMN_NAME_PRODUCTSID = "productsID";
public static abstract class Table_Products implements BaseColumns {
        public static final String COLUMN_NAME_DESCRIPTION = "description";
        public static final String COLUMN_NAME_IMAGE = "image";
        public static final String COLUMN_NAME_SIZE = "size";
        public static final String COLUMN_NAME_PRICE = "price";
        public static final String COLUMN_NAME_QUANTITY = "quantity";
        public static final String COLUMN_NAME_FAVORITE = "favorite";

        public static final String CREATE_TABLE_PRODUCTSS = "CREATE TABLE IF NOT EXISTS " +
                TABLE_NAME_PRODUCTS + " (" +
                COLUMN_NAME_PRODUCTSID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_NAME_DESCRIPTION + TEXT_TYPE + COMMA +
                COLUMN_NAME_IMAGE + BLOB_TYPE + COMMA +
                COLUMN_NAME_SIZE + TEXT_TYPE + COMMA +
                COLUMN_NAME_PRICE + REAL_TYPE + NOT_NULL + COMMA +
                COLUMN_NAME_QUANTITY + INTEGER_TYPE + COMMA +
                COLUMN_NAME_FAVORITE + INTEGER_TYPE + COMMA +
                COLUMN_NAME_MATERIALID + INTEGER_TYPE + NOT_NULL + COMMA +
                COLUMN_NAME_QUANTITYID + INTEGER_TYPE + COMMA +
                COLUMN_NAME_CARTID + INTEGER_TYPE + NOT_NULL + COMMA +
                FOREIGN_KEY + " (" +
                COLUMN_NAME_MATERIALID + ")" + REFERENCES + " " + TABLE_NAME_MATERIAL + " (" + COLUMN_NAME_MATERIALID + ")" +
                ON_UPDATE_CASCADE + COMMA +
                FOREIGN_KEY + " (" +
                COLUMN_NAME_QUANTITYID + ")" + REFERENCES + " " + TABLE_NAME_QUANTITY + " (" + COLUMN_NAME_QUANTITYID + ")" +
                ON_DELETE_SET_NULL + COMMA +
                FOREIGN_KEY + " (" +
                COLUMN_NAME_CARTID + ")" + REFERENCES + " " + TABLE_NAME_CART + " (" + COLUMN_NAME_CARTID + ")" +
                ON_UPDATE_CASCADE + ")";

        public static final String DROP_TABLE_PRODUCTS = "DROP TABLE IF EXIST " + TABLE_NAME_PRODUCTS;
    }

已解决

0 个答案:

没有答案