R
R
rafaelSalihov2020-01-14 08:09:39
Java
rafaelSalihov, 2020-01-14 08:09:39

How to set database table name in constructor for SQLiteOpenHelper?

It seems I am able to use simple constructors if it is not possible to inherit.

public class DatabaseHelper extends SQLiteOpenHelper  {




    public static final String DBNAME = "sample.sqlite";
    public static final String DBLOCATION = "/data/data/plitkurs4.ru.arab/databases/";
    private Context mContext;
    private SQLiteDatabase mDatabase;
    

    public DatabaseHelper(Context context) {
        super(context,  DBNAME,   null, 1);
        this.mContext = context;
    }



    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

    public void openDatabase() {
        String dbPath = mContext.getDatabasePath(DBNAME).getPath();
        if(mDatabase != null && mDatabase.isOpen()) {
            return;
        }
        mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void closeDatabase() {
        if(mDatabase!=null) {
            mDatabase.close();
        }
    }

    public List<Product> getListProduct() {


        Product product = null;
        List<Product> productList = new ArrayList<>();
        openDatabase();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM PRODUCT" , null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            product = new Product(cursor.getInt(0), cursor.getString(1), cursor.getString(3), cursor.getString(2),cursor.getInt(4),cursor.getInt(5));
            productList.add(product);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return productList;
    }
}

There is a DatabaseHelper class, you need to set a change in the PRODUCT database in the constructor, as I understand it, you need to substitute a variable in its place, but then it doesn’t work out how to insert it?
To change the output in another Book_Activity_detal class when accessed.
private DatabaseHelper mDBHelper;
mProductList = mDBHelper.getListProduct(); - to set another here instead of PRODUCT.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question