0

enter image description hereI am trying to either update(if possible) or insert my database in android.I have implemented my code but it's not working as i wanted.As i am new to android, there might be mistake in my code.So the scenario, is there are some items with some quantity,now when i click "ADD TO CART" button this item should be inserted in my database and now when quantity of item is changed then i want to just update my database.But my code always insert a item in new row in the database.Here's my code snippet

addcart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(Integer.parseInt(itemquant.getText().toString())>0){
                    SQLiteDatabase db=cDBHelper.getWritableDatabase();

                    ContentValues values=new ContentValues();
                    values.put(CartContract.CartEntry.CART_NAME,t1.getText().toString());
                    values.put(CartContract.CartEntry.CART_PRICE,Integer.parseInt(t2.getText().toString()));
                    values.put(CartContract.CartEntry.CART_QUANTITY,Integer.parseInt(itemquant.getText().toString()));
                    int id=(int)db.insertWithOnConflict(CartContract.CartEntry.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_IGNORE);
                    if(id==-1)
                    {
                        db.update(CartContract.CartEntry.TABLE_NAME,values, CartContract.CartEntry.CART_NAME+"=?",new String[] {t1.getText().toString()});
                    }
                    Toast.makeText(MainActivity.this,String.valueOf(id),Toast.LENGTH_LONG).show();}
                }
            });
Punit Jain
  • 208
  • 1
  • 2
  • 9
  • Is the key the cartName. Seems like you are updating if there is already a same cart name, does your db have same cart name that you are going to insert now – Psypher Jun 07 '18 at 18:15
  • I want the key to be cartName ,when i click the button the id(int) always increases even though it should be just updated – Punit Jain Jun 07 '18 at 18:22

1 Answers1

0

You should use a DatabaseHelper class that will handle all this logic for you.

Here is a quick example of what a DatabaseHelper class could look like.

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "currencies.db";
private static final String TABLE_NAME = "currency_table";
private static final String TABLE2_NAME = "currency_list_table";

private static final String COL_1 = "ID";
private static final String COL_2 = "SYMBOL";
private static final String COL_3 = "CURRENCY";
private static final String COL_4 = "BALANCE";
private static final String COL_5 = "PRICEPER";
private static final String COL_6 = "IMAGEURL";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, SYMBOL TEXT, CURRENCY TEXT, BALANCE REAL, PRICEPER REAL, IMAGEURL TEXT)" );
    db.execSQL("create table " + TABLE2_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, SYMBOL TEXT, CURRENCY TEXT, IMAGEURL TEXT)" );

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +  TABLE_NAME );
    db.execSQL("DROP TABLE IF EXISTS " +  TABLE2_NAME );
    onCreate(db);
}

public void insertData(boolean isFirstTable, String symbol, String currency, double balance, double pricePer, String imageUrl) {
    SQLiteDatabase db = this.getWritableDatabase();

    /* Creating content values for columns */
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, symbol);
    contentValues.put(COL_3, currency);
    contentValues.put(COL_6, imageUrl);

    if (isFirstTable) {
        contentValues.put(COL_4, balance);
        contentValues.put(COL_5, pricePer);

        /* Inserting data into DB */
        db.insert(TABLE_NAME, null, contentValues);
    }
    else
        db.insert(TABLE2_NAME, null, contentValues);

}

public void deleteData(boolean isFirstTable, String symbol) {
    SQLiteDatabase db = this.getWritableDatabase();

    /* Deletes data by ID */
    if(isFirstTable)
        db.delete(TABLE_NAME, "SYMBOL = ?", new String[] { symbol } );
    else
        db.delete(TABLE2_NAME, "SYMBOL = ?", new String[] { symbol } );


}

public Cursor getAllData(String table) {
    SQLiteDatabase db = this.getWritableDatabase();

    /* Returns all data */
    return db.rawQuery("select * from " + table, null);

}

}

Then to insert data into the database it would be very simple

/* Insertion into DB */
myDB.insertData(true, symbol, chosenCoin, chosenAmount, pricePer, imageUrl);

And to access the database, myDB

myDB = new DatabaseHelper(this);
Cursor res = getDatabase("currency_table");
tlarsin
  • 41
  • 2