0

さて、他の投稿、つまり:

データベース内のテーブルごとに 1 つの SQLiteOpenHelper が必要ですか?

私は、アプリケーションに 1 つ以上のヘルパーを用意することに疑問を抱いていました。まあ、アンサーは 1 つのヘルパー、多くの列でした。

第二部、私は実際にこのチュートリアルを作成しています: http://www.vogella.com/articles/AndroidSQLite/article.html

しかし、私はデータベース上の複数のテーブルでそれを行いたいと思っています。OK、作業を完了し、いわゆるデータ アクセス オブジェクト「DAO」を作成しようとしています。質問は同じで、単一の DAO を使用する方がよいか、テーブルごとに 1 つ (したがってテーブル クラスごとに 1 つ) を使用する方がよいか、または最後に、私が使用できるすべての「アクション」を備えた単一の DAO クラスです。応用... ?

これは、チュートリアルで 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;
  }
} 

前もって感謝します。

4

1 に答える 1

1

これは、アプリケーションがデータベースと対話するのに役立つクラスです。必要なすべてのメソッドを備えた DAO クラスは 1 つだけにする必要があります。(テーブルごとに方法が異なります。)

以下の例を確認してください。

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
于 2012-12-17T12:52:24.253 に答える