-2

I wanted to make a pos, I want to insert a data to the button coming from Sqlite Database. So that when I clicked the button the data name and price showed.

Here is a sample image

public class DatabaseHelper  extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MenuDB";

    //Menu's Tables NAME
    public static final String Table_Product = "Product";

    //Menu's column
    private static final String COL_1 = "ID";
    private static final String COL_2 = "NAME";
    private static final String COL_3 = "PRICE";
    private static final String COL_4 = "TYPE";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CreateTableMenu = "CREATE TABLE IF NOT EXISTS " + Table_Product + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                " NAME TEXT, PRICE TEXT, TYPE TEXT)";
        db.execSQL(CreateTableMenu);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Table_Product);
    }
aboger
  • 2,214
  • 6
  • 33
  • 47

1 Answers1

0

First you should create a method on your DatabaseHelper that accept insert data, like this.

public boolean insertData(String name, String price, String type) { /required parameters
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, name);
        contentValues.put(COL_3, price);
        contentValues.put(COL_4, type);
        db.insert(Table_Product, null, contentValues);

        return true;
    }

then call it like this in your activty whenever you want to add data.

new DatabaseHelper(MainActivity.this).insertData(name, price, type); //supply the parameters.
L2_Paver
  • 596
  • 5
  • 11