首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将插入的数据数增加一倍到Sqlite

将插入的数据数增加一倍到Sqlite
EN

Stack Overflow用户
提问于 2018-03-26 10:47:34
回答 1查看 39关注 0票数 0

我在DB中将两个Office记录插入到Office表中。

代码语言:javascript
运行
复制
  @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();

}

选择所有offices并返回offices列表的getOffices方法:

代码语言:javascript
运行
复制
 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;
}

然后,我使用自定义适配器将数据填充到office RecyclerView中。

虽然我只插入了两个办公室,但为什么返回一个4个办公室的列表?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-26 12:38:09

对于,两个记录的每个 (//Orange//arsema)都有两个插入,如下所示:-

代码语言:javascript
运行
复制
// 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次) :-

代码语言:javascript
运行
复制
//Now just 1 insert
Log.d("Database", 
    "insertBooks: new record id : " + 
        db.insert(DBContract.Office.TABLE_NAME, null, values)
);

P.S.也许对于第二个(//arsema)您应该使用:-

代码语言:javascript
运行
复制
    Bitmap bitmap1 = ((BitmapDrawable) context.getDrawable(R.drawable.aresma)).getBitmap(); //arsema bitmap not orange bitmap
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49489635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档