Reputation: 5063
The application being worked upon uses a static data rather than a dynamic one. So due to this nature I am trying to make a function that would populate the database in a rather efficient way as opposed to writing
INSERT INTO table (id, name, description, category) VALUES ('1', 'ABC', 'XYZ', 'TRY');
The function is as follows:
// Adding medicine entry
public void addMedicine(String brand, String description, String category) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_BRAND, med.getBrand());
values.put(KEY_CATEGORY, med.getCategory());
values.put(KEY_DESCRIPTION, med.getDescription());
db.insert(TABLE_MEDICINE, null, values);
db.close();
}
//populating the database
public void populateDB(){
this.addMedicine(new Medicines("ABC", "XYZ", "ABC"));
this.addMedicine(new Medicines("Mosard Tab", "Each Film coated tablet contains: Mosapride Citrate Dihydrate - 5mg JP eqv. To Mosapride citrate", "Posted under Tablets"));
}
and finally:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_MED_TABLE = "CREATE TABLE " + TABLE_MEDICINE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_BRAND + " TEXT,"
+ KEY_DESCRIPTION + " TEXT," + KEY_CATEGORY + " TEXT )";
db.execSQL(CREATE_MED_TABLE);
populateDB();
}
But this is giving me the following error:
09-18 12:02:29.639: E/AndroidRuntime(13139): FATAL EXCEPTION: main
09-18 12:02:29.639: E/AndroidRuntime(13139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geneticalabs/com.geneticalabs.TrailDB}: java.lang.IllegalStateException: getDatabase called recursively
Could anyone please help ? or suggest any other alternatives on how to populate the database in a non-trivial way.
Also I am using the following function to fetch the data to my app:
//Getting all the medical entries
public List<Medicines> getAll(){
List<Medicines> medList = new ArrayList<Medicines>();
//Select all query
String query = "SELECT * FROM "+TABLE_MEDICINE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(query, null);
if(c.moveToFirst()){
do{
Medicines med = new Medicines();
med.setID(Integer.parseInt(c.getString(0)));
med.setBrand(c.getString(1));
med.setDescription(c.getString(2));
med.setCategory(c.getString(3));
medList.add(med);
}while(c.moveToNext());
}
return medList;
}
Upvotes: 0
Views: 2000
Reputation: 14226
Try reusing your db instance instead fetching a new one with this.getWritableDatabase()
:
public void addMedicine(SQLiteDatabase db, String brand, String description, String category) {
// all your code
// but remove this call: db.close();
}
public void populateDB(SQLiteDatabase db){
this.addMedicine(db, new Medicines("ABC", "XYZ", "ABC"));
this.addMedicine(db, new Medicines("Mosard Tab", "Each Film coated tablet contains: Mosapride Citrate Dihydrate - 5mg JP eqv. To Mosapride citrate", "Posted under Tablets"));
}
@Override
public void onCreate(SQLiteDatabase db) {
// your code
populateDB(db);
}
Upvotes: 2