title: Populating SQLite Database (static)
body: <p>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 </p>
  
  <pre><code>INSERT INTO table (id, name, description, category) VALUES ('1', 'ABC', 'XYZ', 'TRY');
  </code></pre>
  
  <p>The function is as follows:</p>
  
  <pre><code>// 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"));
  }
  </code></pre>
  
  <p>and finally:</p>
  
  <pre><code>@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();
  }
  </code></pre>
  
  <p>But this is giving me the following error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Could anyone please help ? or suggest any other alternatives on how to populate the database in a non-trivial way.</p>
  
  <p>Also I am using the following function to fetch the data to my app:</p>
  
  <pre><code>//Getting all the medical entries
  public List&lt;Medicines&gt; getAll(){
      List&lt;Medicines&gt; medList = new ArrayList&lt;Medicines&gt;();
  
      //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;
  }
  </code></pre>