SQLite数据库表示

时间:2014-11-16 03:29:59

标签: android sqlite

我有一个关于在Android中处理SQLite数据库的问题。

我正在编写一个Application,它正在从SQLite数据库中进行一些数据报告。 这些应用程序包含一个拥有多个片段的活动。 这些片段提供了各种工具来绘制和操作数据并通过接口进行通信。

我还需要一个或多个类来表示我的数据库并提供CRUD函数。

但是现在我不知道在哪里实现这些类。 每个Fragment是否都实现了自己的类,还是应该将它们置于我的Activity中心并通过Interfaces提供CRUD功能? 我会用Interfaces来做,但因为我从来没有用Android做过什么,我不确定。

这种情况是否有推荐的方法或模式?

2 个答案:

答案 0 :(得分:1)

您可以简单地维护SQLiteOpenHelper的单例实例,并在您想要的片段中使用它。

建议将Singleton实例用于以数据为中心的应用程序。即使您有多个线程在同一个数据库实例上工作,也不必担心同步。由于您有一个SQLiteOpenHelper实例,因此您在内部维护SQLiteDatabase对象的单个实例,并且它是线程安全的。

您刚刚提到过活动中的多个片段(未提及有关用于处理数据的线程)。所以我想补充一点。 最好在后台线程上进行以数据为中心的操作。当每个片段对数据进行一些处理时 - 强烈建议在后台线程中进行,以避免阻塞UI /主线程。

要更好地整理代码,请创建DAO图层。

答案 1 :(得分:0)

最好使用该活动来处理数据库活动和写入/阅读,并让您的片段与活动进行通信(无论如何,这都是谷歌推荐的)。你DBOpenHelper看起来像这样:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Dev Paul
 */
public class DataBaseOpenHelper extends SQLiteOpenHelper {



  // Logcat tag
    private static final String LOG = "DatabaseHelper";

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

//Table Names
public static final String REPS_TABLE = "repsTable";


//Reps Table Columns
public static final String R_COLUMN_ID = "repId";
public static final String R_COLUMN_STRING_DATA = "repStringData";
public static final String R_COLUMN_GOOD_FORM = "repGoodForm";

/**
 * Table create statements for the reps objects.
 */
private static final String CREATE_REPS_TABLE = "CREATE TABLE "
        + REPS_TABLE + " (" + R_COLUMN_ID + " INTEGER, "
        + R_COLUMN_STRING_DATA + " TEXT, "
        + R_COLUMN_GOOD_FORM + " INTEGER " + ")";
/**
 * Default constructor.
 * @param context from the calling activity.
 */
public DataBaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_REPS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //drop the tables if they exist.
    db.execSQL("DROP TABLE IF EXISTS " + REPS_TABLE);
    // create new tables
    onCreate(db);
}
}

这是我正在处理的一个应用程序的一部分,最终可能是开源的。然后你的DataSource类看起来像这样......

/**
 * Created by Dev Paul
 */
public class DataSource {

private Context mContext;
private DataBaseOpenHelper dataBaseOpenHelper;
private SQLiteDatabase db;
private boolean isOpened;

/**
 * Class that helps with obtaining data from the data base. This should do all interfacing to
 * the data base. No calls should be directly made to {@code DataBaseOpenHelper}.
 * @param context the context of the calling activity.
 */
public DataSource(Context context) {
    this.mContext = context;
    dataBaseOpenHelper = new DataBaseOpenHelper(context);
}

/**
 * Checks to see if the database has been openned.
 * @return
 */
public boolean isOpened() {
    return isOpened;
}

/**
 * Get a writeable database. This must be called.
 */
public void open() {
    db = dataBaseOpenHelper.getWritableDatabase();
    isOpened = true;
}

/**
 * Close the database. This must be called.
 */
public void close() {
    dataBaseOpenHelper.close();
    isOpened = false;
}

    //handle getting and creating data methods....
}

然后,您只需要使用具有正确SQL语法的查询语句来设置数据设置。因此,在您的活动中,您只需创建一个新的DataSource对象并调用dataSource.open()来打开数据库。然后在您的onDestroy()活动方法中调用dataSource.close(),这样您就不会泄漏任何元素。