首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用SQlite制作数据库应用程序时组织类

如何在使用SQlite制作数据库应用程序时组织类
EN

Stack Overflow用户
提问于 2012-12-17 20:46:44
回答 1查看 739关注 0票数 0

好吧,另一个帖子,我的意思是:

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

我怀疑应用程序是否有一个或多个帮助器,好吧,答案是1个帮助器,许多列。

现在是第二部分,我实际上制作了这个教程:http://www.vogella.com/articles/AndroidSQLite/article.html

但是我想在数据库中使用多个表,好的,我已经完成了工作,现在我正在尝试创建所谓的数据访问对象,"DAO“。问题是一样的,最好只有一个DAO,或者每个表都有一个DAO (每个表类都有一个DAO),或者最后,一个DAO类包含我可以在应用程序中使用的所有“操作”……?

这就是本教程中所说的DAO:

代码语言:javascript
复制
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;
  }
} 

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-17 20:52:24

这个类将帮助您的应用程序与数据库进行交互。您应该只有一个包含所有所需方法的DAO类。(不同的表使用不同的方法。)

检查示例如下:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13914461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档