title: Android not writing to table in database - until
body: <p>I have been practicing a lot with the <code>SQLiteDatabase</code> and <code>SQLiteOpenHelper</code> classes recently, there is something that is puzzling me quite a bit.</p>
  
  <p>I have this <code>constructor</code> and <code>onCreate()</code> method:</p>
  
  <pre><code>public DataBaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  @Override
      public void onCreate(SQLiteDatabase db) {
          String create_table = "CREATE TABLE " + MEASUREMENT_TYPE_TABLE +
                  "(" + idCol + " integer primary key autoincrement, " +
                  typeCol + " varchar(255) not null) ";
  
          db.execSQL(create_table);
  
          populateMeasurementTable();
  
  }
  </code></pre>
  
  <p>the method being called at the end is as below:</p>
  
  <pre><code>public void populateMeasurementTable() {
          SQLiteDatabase db = this.getWritableDatabase();
          for(int i = 0; i &lt; measurementType.length; i++) {
              ContentValues values = new ContentValues();
              values.put(typeCol, measurementType[i]);
              db.insert(MEASUREMENT_TYPE_TABLE, null, values);
          }
  }
  </code></pre>
  
  <p>The point of the method is to populate the table that is created in the <code>onCreate()</code> method. However when I go to <code>tools -&gt; Android -&gt; Monitor (DDMS included)</code> in IntelliJ and look at the database file, it doesn't contain the table or any data (of course).</p>
  
  <p><code>measurementType</code> is simply an array that is being iterated through:</p>
  
  <pre><code>private static final String[] measurementType = new String[] {"acceleration", "angles", "area", "astronomical",
              "density", "energy", "force", "frequency", "length/distance", "power", "pressure", "speed",
              "temperature", "torque", "volume", "weight"};
  </code></pre>
  
  <p>I have tried passing the reference to the database like this:</p>
  
  <p><code>populateMeasurementTable(db);</code></p>
  
  <p>and removing this statement in the method:</p>
  
  <p><code>db = this.getWriteableDatabse();</code></p>
  
  <p>I am pretty sure that it would complain about already having the connection open otherwise. But it still didn't populate the create the table or populate it.</p>
  
  <p>The <code>onCreate()</code> method in the activity is this:</p>
  
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          DataBaseHandler handler = new DataBaseHandler(getApplicationContext());
  }
  </code></pre>
  
  <p>Nothing special, just instantiating the handler class. My understanding is that at that point the <code>onCreate()</code> method would be called and everything would follow through.</p>
  
  <p>Bare in mind that I get no <code>SQLExceptions</code> or <code>errors</code> of any kind.</p>
  
  <p>So I tried this instead in the activity's <code>onCreate()</code> method:</p>
  
  <pre><code>@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          DataBaseHandler handler = new DataBaseHandler(getApplicationContext());
          handler.populateMeasurementTable();
  }
  </code></pre>
  
  <p>I opened the database file from in a <code>SQLite</code> browser and not only was the table there, but the data was in there as well...</p>
  
  <p>So my question I guess is in three fold:</p>
  
  <ul>
  <li>Is simply instantiating the database handler not enough to invoke its <code>onCreate()</code> method? Because that would be why the table isn't created as well as the data not being inserted?</li>
  <li>If instantiating is enough to invoke the method, then why didn't it create the table and populate it with data.</li>
  </ul>
  
  <p>At the moment I am guessing that the first question has the answer (if that makes sense)</p>
  
  <ul>
  <li>Finally for the third question, is there a way that I can simply instantiate it and have it run initialisation methods? Because that populate method is the first of many and I don't really want to be calling them in the activity, doesn't feel like good design habit to be getting into.</li>
  </ul>