vendredi 3 juillet 2015

how to write or insert function and update in sq-lite in android

public void insertData(String cateogry, String status, String feedback,
            String date, Bitmap bitmap) {
        db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(CATEGORY, cateogry);
        cv.put(STATUS, status);
        cv.put(FEEDBACK, feedback);
        cv.put(IMAGE, Utility.getBytes(bitmap));
        cv.put(DATE, date);
        db.insert(INFR_TABLE, CATEGORY, cv);


    }

    public void updateData(String cateogry, String status, String feedback,
            String date, Bitmap bitmap)

    {
        db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(CATEGORY, cateogry);
        cv.put(STATUS, status);
        cv.put(FEEDBACK, feedback);
        cv.put(IMAGE, Utility.getBytes(bitmap));
        cv.put(DATE, date);
        db.update(INFR_TABLE, cv,  "cateogry="+cateogry, null);

    }

this is update and insert funcation in sqlite i want when row of cateogry is alredy there then update it with other value if not then insert . and i am calling like this way :

for(table)

            String data[] = dh.getAllCategory();

    if (Arrays.asList(data).contains("Table")) {
        dh.updateData("Table", t, j, "25.5.2014", BitmapFactory
                .decodeResource(getResources(), R.drawable.ic_launcher));

    }

    else {
        dh.insertData("Table", t, j, "25.5.2014", BitmapFactory
                .decodeResource(getResources(), R.drawable.ic_launcher));

    }

for(water) :

String data[] = dh.getAllCategory();
    // dh.deleteitem(11);
    if (Arrays.asList(data).contains("Table")) {
        dh.updateData("Water", t, j, "25.5.2014", BitmapFactory
                .decodeResource(getResources(), R.drawable.ic_launcher));

    }

    else
    {
        dh.insertData("Water", t, j, "25.5.2014", BitmapFactory
                .decodeResource(getResources(), R.drawable.ic_launcher));

    }

but every time its create new row and duplicate please help me where am doing wrong we have 2 class in on is for table and another for water. please tell

Aucun commentaire:

Enregistrer un commentaire