0

I have this code below which consist on my Android app DatabaseHelper Class, Currently i have hardcoded a row of data inside my database which is the Barcode(263924) and Quantity(1).

But the problem is i'm currently trying to make it so that when the user adds the same barcode number i hardcoded into my database the quantity will +1 and not create a new row.

Sorry im currently new to android any help would be greatly appreciated thanks!

MainActivity Codes

@Override
public void handleResult(Result result) {
    final String barcodes = result.getText();
    final String quantitys = "1";
    Log.d("QRCodeScanner", result.getText());
    Log.d("QRCodeScanner", result.getBarcodeFormat().toString());

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Scan Result");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            scannerView.resumeCameraPreview(MainActivity.this);
        }
    });
    builder.setMessage(result.getText());
    AlertDialog alert1 = builder.create();
    alert1.show();
    AddData(barcodes,quantitys);

}

public void AddData(String barcodes,String quantitys){
    boolean insertData = myDB.addData(barcodes,quantitys);

    if(insertData==true){
        Toast.makeText(MainActivity.this,"Successfully Entered Data!",Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(MainActivity.this,"Something went wrong :(.",Toast.LENGTH_LONG).show();
    }
}

Database Code

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "scan.db";
    public static final String TABLE_NAME = "scan_data";
    public static final String COL1 = "ID";
    public static final String COL2 = "BARCODE";
    public static final String COL3 = "QUANTITY";
    public static final String COL4 = "TIMESTAMP";



    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " BARCODE TEXT, QUANTITY TEXT, TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP)";
        db.execSQL(createTable);

        ContentValues contentValues=new ContentValues();
        contentValues.put(DatabaseHelper.COL2,263924);
        contentValues.put(DatabaseHelper.COL3,1);

        db.insert(DatabaseHelper.TABLE_NAME,null,contentValues);


    }

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

    public boolean addData(String code, String quant) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, code);

        String[] params = new String[]{code};
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COL2 + " = ?", params);
        long result;
        if (data.getCount() > 0) {
            data.moveToFirst();
            String[] args = new String[]{data.getString(0)};
            contentValues.put(COL3, (Integer.parseInt(data.getString(2)) + Integer.parseInt(quant)));
            result = db.update(TABLE_NAME, contentValues, COL1 + "=?", args);
        } else {
            contentValues.put(COL3, quant);
            result = db.insert(TABLE_NAME, null, contentValues);
        }
        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getListContents() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }
}
Best Jeanist
  • 1,109
  • 4
  • 14
  • 34
  • what is your question? – Kiran Malvi Nov 05 '18 at 06:52
  • @KiranMalvi hardcoded a row of data in my database Barcode(263924) and Quantity(1) i want to make it so that when the user adds in a same barcode number the quantity will plus 1. else if its a different barcode number it will just insert – Best Jeanist Nov 05 '18 at 06:56

1 Answers1

1

Try below,

public boolean addData(String code, String quant) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, code);
    String[] params = new String[]{code};
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COL2 + " = ?", params);
    long result;
    if (data.getCount() > 0) {
        data.moveToFirst();
        String[] args = new String[]{data.getString(0)};
        contentValues.put(COL3, (Integer.parseInt(data.getString(2)) + Integer.parseInt(quant)));
        result = db.update(TABLE_NAME, contentValues, COL1 + "=?", args);
    } else {
        contentValues.put(COL3, quant);
        result = db.insert(TABLE_NAME, null, contentValues);
    }
    //if date as inserted incorrectly it will return -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

UPDATE

For time stamp update use below code:

contentValues.put(COL4, " time('now') ");
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62