Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question