将插入数据的数量加倍到Sqlite

时间:2018-03-26 10:47:35

标签: android android-sqlite

我将2个办公室记录插入MY DB中的Office表。

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void insertOffices(SQLiteDatabase db) {

    ContentValues values = new ContentValues();

    //Insert Orange tours.
    Bitmap bitmap = ((BitmapDrawable) context.getDrawable(R.drawable.orange)).getBitmap();
    byte[] temp = convertImage.bitmapToByte(bitmap);
    values.put(DBContract.Office.COL_OFFNAME, "اورنج تورز");
    values.put(DBContract.Office.COL_OFFIMAGE, temp);
    values.put(DBContract.Office.COL_OFFDESC, "شركة سياحة وسفر");
    values.put(DBContract.Office.COL_OFFLOCATION, "بديا" + "الشارع الرئيسي");
    values.put(DBContract.Office.COL_OFFPHONE, "0554544545");
    values.put(DBContract.Office.COL_OFFRATE, "10");

    long newRowId;
    newRowId = db.insert(
            DBContract.Office.TABLE_NAME,
            null,
            values);

    Log.d("Database", "insertBooks: new record id : " + newRowId);

    db.insert(DBContract.Office.TABLE_NAME, null, values);

    values.clear();

    //Insert arsema
    Bitmap bitmap1 = ((BitmapDrawable) context.getDrawable(R.drawable.orange)).getBitmap();
    byte[] temp1 = convertImage.bitmapToByte(bitmap1);
    values.put(DBContract.Office.COL_OFFNAME, "ارسيما");
    values.put(DBContract.Office.COL_OFFIMAGE, temp1);
    values.put(DBContract.Office.COL_OFFDESC, "شركة سياحة وسفر");
    values.put(DBContract.Office.COL_OFFLOCATION, "جنين" + "الشارع الرئيسي");
    values.put(DBContract.Office.COL_OFFPHONE, "0554544545");
    values.put(DBContract.Office.COL_OFFRATE, "10");


    newRowId = db.insert(
            DBContract.Office.TABLE_NAME,
            null,
            values);

    Log.d("Database", "insertBooks: new record id : " + newRowId);


    db.insert(DBContract.Office.TABLE_NAME, null, values);

    values.clear();

}

getOffices方法,选择所有办公室并返回办公室列表:

 public List<Office> getOffices() {
    List<Office> offices = new ArrayList<Office>();

    String selectQuery = "SELECT  * FROM " + DBContract.Office.TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor!=null && cursor.moveToFirst()) {
        do {
            Office office = new Office();
            office.setOfficeID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBContract.Office._ID))));
            office.setOfficeName(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFNAME)));
            office.setOfficeLocation(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFLOCATION)));
            office.setOfficeImage(cursor.getBlob(cursor.getColumnIndex(DBContract.Office.COL_OFFIMAGE)));
            office.setOfficeDescreption(cursor.getString(cursor.getColumnIndex(DBContract.Office.COL_OFFDESC)));
            office.setOfficeRate(cursor.getInt(cursor.getColumnIndex(DBContract.Office.COL_OFFRATE)));

            // Adding offices to list
            offices.add(office);
        } while (cursor.moveToNext());
    }

    return offices;
}

然后我使用自定义适配器将数据填充到办公室活动RecyclerView

为什么这会返回4个办事处的清单,虽然我只插入了2个办事处?

1 个答案:

答案 0 :(得分:0)

对于两个记录中的每个 //橙色 // arsema ),您有两个插入按照: -

// 1st insert
newRowId = db.insert(
        DBContract.Office.TABLE_NAME,
        null,
        values);

Log.d("Database", "insertBooks: new record id : " + newRowId);


//Duplicate insert (albeit with different id)
db.insert(DBContract.Office.TABLE_NAME, null, values);

而是使用类似的东西(即每个只插入1个): -

//Now just 1 insert
Log.d("Database", 
    "insertBooks: new record id : " + 
        db.insert(DBContract.Office.TABLE_NAME, null, values)
);

P.S。也许是你应该使用的第二个(// arsema): -

    Bitmap bitmap1 = ((BitmapDrawable) context.getDrawable(R.drawable.aresma)).getBitmap(); //arsema bitmap not orange bitmap