2016-07-12 8 views
0

sqliteデータベースを作成しました。問題なくデータが挿入されます。このテーブルに5つのフィールドが挿入されています。データはadpater.Ifのplus imageviewをクリックした後に挿入されます。私はアダプターのページに一度来て、その時点で私はレストランIDが既にテーブルに存在していることを確認したい。データベース内の利用可能なIDと来るIDが等しいということは、値が特定のIDに対して更新されることを意味する。それ以外の場合は、すべてのデータがテーブルから削除されます。削除されたqureyを使用して操作が実装されました。sqliteの中の特定のものを削除してください

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(); 
    } 

} 

アダプタページ

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(); 
      } 
     }); 
+0

ので、あなたは何をしたいです?テーブル全体を削除したいですか? –

+1

@Abserve Techはこのdb.execSQL( "delete from" + TABLE_FOOD)を使用します。あなたのコードが見えないので、削除されてしまうと思います。 – Nisarg

+0

はい、私はすべてのテーブルを削除し、別のres_idが来たら再びテーブルを作り直したいです。 –

答えて

1

それはdb.execSQL( "TABLE_FOOD。DELETE FROM")ではありません。

正しい構文は次のとおりです。

db.execSQL("DELETE FROM " + TABLE_FOOD); 
関連する問題