How to organize classes while making a database app with SQlite

well, the other post, i mean :

Should there be one SQLiteOpenHelper for each table in the database?

i was doubtful about having one or more Helpers for the application, well, the anwser was 1 helper, many columns.

now the second part, i'm actually making this tutorial: http://www.vogella.com/articles/AndroidSQLite/article.html

but i would like to do it with more than 1 table on the database, ok, i've done the work and now i'm try to make the so called Data Access Object, "DAO". the question is the same, is better to have a single DAO, or is better to have one for each table (and so one for each table class) OR lastly, a single DAO class with all the "actions" i could use in the application... ?

this is what in the tutorial is called DAO:

package de.vogella.android.sqlite.first;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CommentsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
      MySQLiteHelper.COLUMN_COMMENT };

  public CommentsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(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(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
        allColumns, MySQLiteHelper.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(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Comment> getAllComments() {
    List<Comment> comments = new ArrayList<Comment>();

    Cursor cursor = database.query(MySQLiteHelper.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;
  }
} 

Thanks in advance.

Upvotes: 0

Views: 787

Answers (1)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

This is the class that will help your application to interact with your database. You should have only one DAO class with all methods you need. (Different methods for different tables.)

Check example below:

public void insertInTableA (String[]) //or any args
{
    //Logic for table A row insertion
}

public void insertInTableB (String[]) //or any args
{
    //Logic for table B row insertion
}

public void dltFromTableA (String where) //or any args
{
    //Logic for table A row deletion
}

public void dltFromTableB (String where) //or any args
{
    //Logic for table B row deletion
}

//Other function as per requirement

Upvotes: 1

Related Questions