title: Android SQLite not working in separate classes
body: <p>I have a <code>SQLite</code> database setup for my android application and I have it working in one class where I can write information into the database. My long term plan is to allow me to have on database seup and have different classes access and updating the information the database is set up with these classes:</p>
  
  <pre><code>public class DataBaseHelper extends SQLiteOpenHelper {
  
  public static final String TABLE_COMMENTS = "comments";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_COMMENT = "comment";
  
  private static final String DATABASE_NAME = "commments.db";
  private static final int DATABASE_VERSION = 1;
  
  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
          + TABLE_COMMENTS + "( " + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMMENT
          + " text not null);";
  
  public DataBaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase database) {
      database.execSQL(DATABASE_CREATE);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(DataBaseHelper.class.getName(),
              "Upgrading database from version " + oldVersion + " to "
                      + newVersion + ", which will destroy all old data");
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
      onCreate(db);
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>   public class Comment {
  private long id;
  private String comment;
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public String getComment() {
      return comment;
  }
  
  public void setComment(String comment) {
      this.comment = comment;
  }
  
  // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
      return comment;
  }
   }
  
      public class CommentsDataSource {
  
  // Database fields
  private SQLiteDatabase database;
  private DataBaseHelper dbHelper;
  private String[] allColumns = { DataBaseHelper.COLUMN_ID,
          DataBaseHelper.COLUMN_COMMENT };
  
  public CommentsDataSource(Context context) {
      dbHelper = new DataBaseHelper(context);
  }
  
  public void open() throws SQLException {
      database = dbHelper.getWritableDatabase();
  }
  
  public void close() {
      dbHelper.close();
  }
  
  public Comment createComment(String comment) {
      ContentValues values = new ContentValues();
      values.put(DataBaseHelper.COLUMN_COMMENT, comment);
      long insertId = database.insert(DataBaseHelper.TABLE_COMMENTS, null,
              values);
      Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS,
              allColumns, DataBaseHelper.COLUMN_ID + " = " + insertId, null,
              null, null, null);
      cursor.moveToFirst();
      Comment newComment = cursorToComment(cursor);
      cursor.close();
      return newComment;
  }
  
  public void deleteComment(Comment comment) {
      long id = comment.getId();
      System.out.println("Comment deleted with id: " + id);
      database.delete(DataBaseHelper.TABLE_COMMENTS, DataBaseHelper.COLUMN_ID
              + " = " + id, null);
  }
  
  public List&lt;Comment&gt; getAllComments() {
      List&lt;Comment&gt; comments = new ArrayList&lt;Comment&gt;();
  
      Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS,
              allColumns, null, null, null, null, null);
  
      cursor.moveToFirst();
      while (!cursor.isAfterLast()) {
          Comment comment = cursorToComment(cursor);
          comments.add(comment);
          cursor.moveToNext();
      }
      // Make sure to close the cursor
      cursor.close();
      return comments;
  }
  
  private Comment cursorToComment(Cursor cursor) {
      Comment comment = new Comment();
      comment.setId(cursor.getLong(0));
      comment.setComment(cursor.getString(1));
      return comment;
  }
   }
  </code></pre>
  
  <p>in my class that is working i have:</p>
  
  <pre><code>    public class WorkoutProgress extends ListActivity {
  private CommentsDataSource datasource;
  
  @Override
  
  public void onCreate(Bundle savedInstanceState) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.progress);
  
      datasource = new CommentsDataSource(this);
      datasource.open();
  
      List&lt;Comment&gt; values = datasource.getAllComments();
  
      // Use the SimpleCursorAdapter to show the
      // elements in a ListView
      ArrayAdapter&lt;Comment&gt; adapter = new ArrayAdapter&lt;Comment&gt;(this,
              android.R.layout.simple_list_item_1, values);
      setListAdapter(adapter);
  }
  
  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
      @SuppressWarnings("unchecked")
      ArrayAdapter&lt;Comment&gt; adapter = (ArrayAdapter&lt;Comment&gt;) getListAdapter();
      Comment comment = null;
      switch (view.getId()) {
      case R.id.add:
          String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
          int nextInt = new Random().nextInt(3);
          // Save the new comment to the database
          comment = datasource.createComment(""+49);
          adapter.add(comment);
          break;
      case R.id.delete:
          if (getListAdapter().getCount() &gt; 0) {
              comment = (Comment) getListAdapter().getItem(0);
              datasource.deleteComment(comment);
              adapter.remove(comment);
          }
          break;
      }
      adapter.notifyDataSetChanged();
  }
  
  @Override
  protected void onResume() {
      datasource.open();
      super.onResume();
  }
  
  @Override
  protected void onPause() {
      datasource.close();
      super.onPause();
  }
  </code></pre>
  
  <p>}
  i tried to use the code that is used to write to the database in a different class but it doesnt work any idea why?</p>