Abserve Tech
Abserve Tech

Reputation: 117

Delete the particular in sqlite android

I have created the sqlite database. Initially, the data are inserted without any problem. I have inserted five columns in that table. The data are inserted after I have clicked the plus imageview in adapter. If I come once into the adapter page, at that time I want to check that restaurant id is already present in the table. The available id in database and coming id is equal means the value get updated for particular id. Otherwise all the data is deleted from the table. I have implemented the operation using the delete query. The rows are deleted if I use that query after that the value not inserted into the already created table.

    public class Database_handler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION =19;
    // Database Name
    private static final String DATABASE_NAME = "Foodcheckin";
    // Contacts table name
    private static final String TABLE_FOOD = "Food_table";
    // Shops Table Columns names
    private static final String RES_ID = "res_id";
    private static final String FOOD_ID = "id";
    private static final String FOOD_NAME = "name";
    private static final String FOOD_AMOUNT = "amount";
    private static final String FOOD_COUNT = "count";

    public Database_handler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FOOD + "("+ RES_ID + " TEXT,"
                + FOOD_ID + " TEXT,"+  FOOD_NAME + " TEXT,"
                + FOOD_AMOUNT + " TEXT,"  + FOOD_COUNT + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);

// Creating tables again
       onCreate(db);
    }
    // Adding new shop

    public void addShop(Shop shop) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(RES_ID, shop.getfoodid());
        values.put(FOOD_ID, shop.getfoodid());
        values.put(FOOD_NAME, shop.getfoodname());
        values.put(FOOD_AMOUNT, shop.getfoodamount());
        values.put(FOOD_COUNT, shop.getfoodcount());


       // db.insertWithOnConflict(TABLE_FOOD, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        db.insert(TABLE_FOOD, null, values);
        db.close(); // Closing database connection
    }

    public void deleteallrow() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM TABLE_FOOD");
    }

    public List<Shop> getAllShops() {
        List<Shop> shopList = new ArrayList<Shop>();
        Checkinpage.shopList1= new ArrayList<Shop>();
// Select All Query
        String selectQuery = "SELECT * FROM " + TABLE_FOOD;

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

// looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Shop shop = new Shop();
                shop.setresid((cursor.getString(0)));
                shop.setfoodid((cursor.getString(1)));
                shop.setfoodname(cursor.getString(2));
                shop.setfoodamount(cursor.getString(3));
                shop.setfoodcount(cursor.getString(4));
// Adding contact to list
                shopList.add(shop);
                Checkinpage.shopList1.add(shop);
            } while (cursor.moveToNext());
        }


// return contact list
        return shopList;
    }

    public int updateShop(Shop shop) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(FOOD_ID, shop.getfoodid());
        values.put(FOOD_NAME, shop.getfoodname());
        values.put(FOOD_AMOUNT, shop.getfoodamount());
        values.put(FOOD_COUNT, shop.getfoodcount());

// updating row
        return db.update(TABLE_FOOD, values, FOOD_ID + " = ?",
                new String[]{String.valueOf(shop.getfoodid())});
    }

    public void deleteShop(Shop shop) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_FOOD, FOOD_ID + " = ?",
                new String[] { String.valueOf(shop.getfoodid()) });
        db.close();
    }

}

adapter page

viewHolder.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                checkin_layout.setVisibility(View.VISIBLE);
                if(modelChild.getCount()>=0)  // set your count default 0 when you bind data initially
                {
                    int count = (modelChild.getCount()) + 1;
                    modelChild.setCount(count);
                    count1=count1+1;
                    int s= Integer.parseInt(Detailpage.item.getText().toString());
                    Log.d("s--", String.valueOf(s));
                   //count1=s+count1;
                    itemname.setText(Integer.toString(count1));
                   // viewHolder.txtView.setText(Integer.toString(count1)+"items");

                    Detailpage.item.setText(Integer.toString(count1));
                    int foodprice=0;
                     foodprice=Integer.parseInt(child.getPrice());

                    int total = foodprice * child.getcount();
                    String total1=Integer.toString(total);
                    String value=Integer.toString(modelChild.getcount());
//
                   String name = modelChild.getName();

                    String id=child.getId();


                    if (res_id != null) {
                        try{

                            shops = db.getAllShops();
                            for (Shop shop : shops) {

                            if(res_id !=shop.getresid()){
                                db.deleteallrow();  
                            }
                                else{
                                db.addShop(new Shop(res_id,id,name, total1,value));}

                            }



                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }




                    }

                }


                // set your other items if any like above
                groups.get(groupPosition).getItems().set(childPosition, modelChild);
                notifyDataSetChanged();
            }
        });

Upvotes: 0

Views: 70

Answers (1)

Nishan Khadka
Nishan Khadka

Reputation: 414

It's not db.execSQL("DELETE FROM TABLE_FOOD");

Correct syntax is:

  db.execSQL("DELETE FROM " + TABLE_FOOD);

Upvotes: 1

Related Questions