mercredi 2 décembre 2015

Trouble Updating SQLite Database Row

I have been stuck at this problem for a while now. I am selecting an item from a list which is created from the items in the SQLite database. On item click makes an array of data for this item. What i am trying to do is every time the make drink button is pressed I want the counter column of that item in the database to increase by one. The code for updating the values is right since i have used the same code to upgrade the database rows in other databases and there is no error when i run the program. My problem is that the data is not getting updated in the database. (I have done my research and gone through all the post like this on this site and others but I c'ant really find a solution to my problem)

Here is my DatabaseHandler Code:

public class DatabaseHandler extends SQLiteOpenHelper {
//
    private final ArrayList<MyCocktail> cocktailList = new ArrayList<>();

public DatabaseHandler(Context context) {
    super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase db) {

    //create our table columns
    String CREATE_COCKTAILS_TABLE = "CREATE TABLE "+ Constants.TABLE_NAME +
            " ("+
            Constants.KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+
            Constants.NAME_COCKTAIL +" TEXT,"+
            Constants.DESCRIPTION_COCKTAIL+" TEXT,"+
            Constants.GIN+" TEXT DEFAULT (0),"+
            Constants.RUM+" TEXT DEFAULT (0),"+
            Constants.TEQUILA+" TEXT DEFAULT (0),"+
            Constants.VODKA+" TEXT DEFAULT (0),"+
            Constants.DRY_VERMOUTH+" TEXT DEFAULT (0),"+
            Constants.WHISKEY+" TEXT DEFAULT (0),"+
            Constants.SODA+" TEXT DEFAULT (0),"+
            Constants.COLA+" TEXT DEFAULT (0),"+
            Constants.A7UP+" TEXT DEFAULT (0),"+
            Constants.WATER+" TEXT DEFAULT (0),"+
            Constants.LIME_JUICE+" TEXT DEFAULT (0),"+
            Constants.ORANGE_JUICE+" TEXT DEFAULT (0),"+
            Constants.PINEAPPLE_JUICE+" TEXT DEFAULT (0),"+
            Constants.TOMATO_JUICE+" TEXT DEFAULT (0),"+
            Constants.CRANBERRY_JUICE+" TEXT DEFAULT (0),"+
            Constants.ADDON_COCKTAIL+" TEXT ,"+
            Constants.GARNISH_COCKTAIL+" TEXT ,"+
            Constants.GLASS_COCKTAIL+" TEXT,"+
            Constants.IMAGE_COCKTAIL+" TEXT NOT NULL,"+
            Constants.COUNTER+" TEXT "+
            ");";

    db.execSQL(CREATE_COCKTAILS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS "+Constants.TABLE_NAME);

    //create a new table
    onCreate(db);

}

//to delete a cocktail
public void deleteCocktail(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Constants.TABLE_NAME, Constants.KEY_ID + "=?",
            new String[]{String.valueOf(id)});
    db.close();
}

//add content to created table
//MyCocktail refering to the created java class MyCocktail
public void addCocktails( MyCocktail cocktail){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constants.NAME_COCKTAIL, cocktail.getcName());
    values.put(Constants.DESCRIPTION_COCKTAIL, cocktail.getDescription());
    values.put(Constants.GIN, cocktail.getGinQuant());
    values.put(Constants.RUM, cocktail.getRumQuant());
    values.put(Constants.TEQUILA, cocktail.getTequilaQuant());
    values.put(Constants.VODKA, cocktail.getVodkaQuant());
    values.put(Constants.DRY_VERMOUTH, cocktail.getDryVermouthQuant());
    values.put(Constants.WHISKEY, cocktail.getWhiskeyQuant());
    values.put(Constants.SODA, cocktail.getSodaQuant());
    values.put(Constants.COLA, cocktail.getColaQuant());
    values.put(Constants.A7UP, cocktail.getA7upQuant());
    values.put(Constants.WATER, cocktail.getWaterQuant());
    values.put(Constants.LIME_JUICE, cocktail.getLimeJuiceQuant());
    values.put(Constants.ORANGE_JUICE, cocktail.getOrangeJuiceQuant());
    values.put(Constants.PINEAPPLE_JUICE, cocktail.getPineappleJuiceQuant());
    values.put(Constants.TOMATO_JUICE, cocktail.getTomatoJuiceQuant());
    values.put(Constants.CRANBERRY_JUICE, cocktail.getCranberryJuiceQuant());
    values.put(Constants.ADDON_COCKTAIL, cocktail.getAddon());
    values.put(Constants.GARNISH_COCKTAIL, cocktail.getGarnish());
    values.put(Constants.GLASS_COCKTAIL, cocktail.getGlass());
    values.put(Constants.IMAGE_COCKTAIL, cocktail.getImage());
    values.put(Constants.COUNTER, "0");

    //insert the values into the database table
    db.insert(Constants.TABLE_NAME, null, values);
    //close the database table
    db.close();
}

//update cocktail wrt id
public void updateCocktails(MyCocktail cocktail,int id){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constants.NAME_COCKTAIL, cocktail.getcName());
    values.put(Constants.DESCRIPTION_COCKTAIL, cocktail.getDescription());
    values.put(Constants.GIN, cocktail.getGinQuant());
    values.put(Constants.RUM, cocktail.getRumQuant());
    values.put(Constants.TEQUILA, cocktail.getTequilaQuant());
    values.put(Constants.VODKA, cocktail.getVodkaQuant());
    values.put(Constants.DRY_VERMOUTH, cocktail.getDryVermouthQuant());
    values.put(Constants.WHISKEY, cocktail.getWhiskeyQuant());
    values.put(Constants.SODA, cocktail.getSodaQuant());
    values.put(Constants.COLA, cocktail.getColaQuant());
    values.put(Constants.A7UP, cocktail.getA7upQuant());
    values.put(Constants.WATER, cocktail.getWaterQuant());
    values.put(Constants.LIME_JUICE, cocktail.getLimeJuiceQuant());
    values.put(Constants.ORANGE_JUICE, cocktail.getOrangeJuiceQuant());
    values.put(Constants.PINEAPPLE_JUICE, cocktail.getPineappleJuiceQuant());
    values.put(Constants.TOMATO_JUICE, cocktail.getTomatoJuiceQuant());
    values.put(Constants.CRANBERRY_JUICE, cocktail.getCranberryJuiceQuant());
    values.put(Constants.ADDON_COCKTAIL, cocktail.getAddon());
    values.put(Constants.GARNISH_COCKTAIL, cocktail.getGarnish());
    values.put(Constants.GLASS_COCKTAIL, cocktail.getGlass());
    values.put(Constants.IMAGE_COCKTAIL, cocktail.getImage());
    values.put(Constants.COUNTER, cocktail.getCounter());

    //insert the values into the database table
    db.update(Constants.TABLE_NAME, values, Constants.KEY_ID + " =?" , new String[]{String.valueOf(cocktail.getItemId())});

    //close the database table
    db.close();
}

And here the code where i am trying to update the database row:

    makeDrinkButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String array=getMakeDrinkArray();

            int id = tempCocktail.getItemId();
            int tempCounter;
            tempCounter = Integer.parseInt(tempCocktail.getCounter());
            tempCounter += 1;
            tempCocktail.setCounter(Integer.toString(tempCounter));
            Toast.makeText(getApplicationContext(), tempCocktail.getCounter(), Toast.LENGTH_LONG).show();
            DatabaseHandler dBHandler = new DatabaseHandler(CocktailMenuActivity.this);
            dBHandler.updateCocktails(tempCocktail,id);

            try {
                refreshData();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

The tempCocktail object is being created by clicking on the item of the list.Here is the code for that:

private void refreshData() throws IOException {

//Creating cursor to populate list view
    //DatabaseHandler is a SQLiteOpenHelper class connecting to SQLite
    DatabaseHandler dbHandler = new DatabaseHandler(this);
    // Get access to the underlying writeable database
    SQLiteDatabase db = dbHandler.getWritableDatabase();
    // Get access to the underlying writeable database
    Cursor listCursor = db.rawQuery("SELECT * FROM " + Constants.TABLE_NAME+";", null);


// Setup cursor adapter using cursor from last step
    CocktailCursorAdapter cocktailCursorAdapter = new CocktailCursorAdapter(this, listCursor);
// Attach cursor adapter to the ListView
    listView.setAdapter(cocktailCursorAdapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //setting up the cursor to retrieve data from the list item
            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            //setting drink name for cocktail activity
            tempCocktail.setcName(cursor.getString(cursor.getColumnIndex(Constants.NAME_COCKTAIL)));
            drinkName.setText(tempCocktail.getcName());
            //setting drink description for cocktail activity
            tempCocktail.setDescription(cursor.getString(cursor.getColumnIndex(Constants.DESCRIPTION_COCKTAIL)));
            drinkDescription.setText(tempCocktail.getDescription());
            //setting addon value for cocktail activity
            tempCocktail.setAddon(cursor.getString(cursor.getColumnIndex(Constants.ADDON_COCKTAIL)));
            drinkAddon.setText("Add-On: " + tempCocktail.getAddon());
            //setting garnish value for cocktail activity
            tempCocktail.setGarnish(cursor.getString(cursor.getColumnIndex(Constants.GARNISH_COCKTAIL)));
            drinkGarnish.setText("Garnish: " + tempCocktail.getGarnish());
            //setting glass value for cocktail activity
            tempCocktail.setGlass(cursor.getString(cursor.getColumnIndex(Constants.GLASS_COCKTAIL)));
            drinkGlass.setText("Glass:" + tempCocktail.getGlass());
            //setting image for Cocktail activity
            tempCocktail.setImage(cursor.getString(cursor.getColumnIndex(Constants.IMAGE_COCKTAIL)));
            Context context1 = imageView.getContext();
            int imgId = context1.getResources().getIdentifier(tempCocktail.getImage(), "drawable", context1.getPackageName());
            imageView.setImageResource(imgId);

            tempCocktail.setItemId(cursor.getColumnIndex(Constants.KEY_ID));
            tempCocktail.setCounter(cursor.getString(cursor.getColumnIndex(Constants.COUNTER)));

            //Strings containing quantity of the drinks
            tempCocktail.setGinQuant(cursor.getString(cursor.getColumnIndex(Constants.GIN)));
            tempCocktail.setRumQuant(cursor.getString(cursor.getColumnIndex(Constants.RUM)));
            tempCocktail.setTequilaQuant(cursor.getString(cursor.getColumnIndex(Constants.TEQUILA)));
            tempCocktail.setVodkaQuant(cursor.getString(cursor.getColumnIndex(Constants.VODKA)));
            tempCocktail.setDryVermouthQuant(cursor.getString(cursor.getColumnIndex(Constants.DRY_VERMOUTH)));
            tempCocktail.setWhiskeyQuant(cursor.getString(cursor.getColumnIndex(Constants.WHISKEY)));
            tempCocktail.setSodaQuant(cursor.getString(cursor.getColumnIndex(Constants.SODA)));
            tempCocktail.setColaQuant(cursor.getString(cursor.getColumnIndex(Constants.COLA)));
            tempCocktail.setA7upQuant(cursor.getString(cursor.getColumnIndex(Constants.A7UP)));
            tempCocktail.setWaterQuant(cursor.getString(cursor.getColumnIndex(Constants.WATER)));
            tempCocktail.setLimeJuiceQuant(cursor.getString(cursor.getColumnIndex(Constants.LIME_JUICE)));
            tempCocktail.setOrangeJuiceQuant(cursor.getString(cursor.getColumnIndex(Constants.ORANGE_JUICE)));
            tempCocktail.setPineappleJuiceQuant(cursor.getString(cursor.getColumnIndex(Constants.PINEAPPLE_JUICE)));
            tempCocktail.setTomatoJuiceQuant(cursor.getString(cursor.getColumnIndex(Constants.TOMATO_JUICE)));
            tempCocktail.setCranberryJuiceQuant(cursor.getString(cursor.getColumnIndex(Constants.CRANBERRY_JUICE)));

        }
    });
}

There is no error which i am getting from the program. The only problem is that the data is not being updated into the database. Any and all help will be appreciated. Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire