簡體   English   中英

當前推薦哪種SQLite設計模式?

[英]Which SQLite design pattern is currently recommended?

我當時在考慮為我的應用程序所擁有的數據庫實現單例設計模式,而不是經歷制作和銷毀數據庫實例的昂貴操作。

但是,不久前,我讀到Android和SQLite有兩個選項(但是我不記得是什么,也找不到鏈接),所以我想知道推薦哪種設計模式這些數據庫的優點/缺點是什么?

謝謝,利亞姆

我用這個方法。 我使用這些單例類SQLiteHelper,SelectionBuilder。

public class SQLiteHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "test.db";

public static final String PRIMARY_KEY = " PRIMARY KEY";
public static final String TYPE_TEXT = " TEXT";
public static final String TYPE_INTEGER = " INTEGER";
public static final String TYPE_REAL = " REAL";
public static final String COMMA_SEP = ",";

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Category.SQL_CREATE_TABLE);
    db.execSQL(Article.SQL_CREATE_TABLE);
    db.execSQL(IntroImage.SQL_CREATE_TABLE);
    db.execSQL(Asset.SQL_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(Category.SQL_DROP_TABLE);
    db.execSQL(Article.SQL_DROP_TABLE);
    db.execSQL(IntroImage.SQL_DROP_TABLE);
    db.execSQL(Asset.SQL_DROP_TABLE);
}

}

SQLiteOpenHelper是用於數據庫創建和升級的單例。 顧名思義,SelectionBuilder是構建選擇子句的助手。

public class SelectionBuilder {
private static final String TAG = SelectionBuilder.class.getSimpleName();

private String mTable = null;
private Map<String, String> mProjectionMap = Maps.newHashMap();
private StringBuilder mSelection = new StringBuilder();
private ArrayList<String> mSelectionArgs = Lists.newArrayList();

/**
 * Reset any internal state, allowing this builder to be recycled.
 */
public SelectionBuilder reset() {
    mTable = null;
    mSelection.setLength(0);
    mSelectionArgs.clear();
    return this;
}

/**
 * Append the given selection clause to the internal state. Each clause is
 * surrounded with parenthesis and combined using {@code AND}.
 */
public SelectionBuilder where(String selection, String... selectionArgs) {
    if (TextUtils.isEmpty(selection)) {
        if (selectionArgs != null && selectionArgs.length > 0) {
            throw new IllegalArgumentException(
                    "Valid selection required when including arguments=");
        }

        // Shortcut when clause is empty
        return this;
    }

    if (mSelection.length() > 0) {
        mSelection.append(" AND ");
    }

    mSelection.append("(").append(selection).append(")");
    if (selectionArgs != null) {
        Collections.addAll(mSelectionArgs, selectionArgs);
    }

    return this;
}

public SelectionBuilder table(String table) {
    mTable = table;
    return this;
}

private void assertTable() {
    if (mTable == null) {
        throw new IllegalStateException("Table not specified");
    }
}

public SelectionBuilder mapToTable(String column, String table) {
    mProjectionMap.put(column, table + "." + column);
    return this;
}

public SelectionBuilder map(String fromColumn, String toClause) {
    mProjectionMap.put(fromColumn, toClause + " AS " + fromColumn);
    return this;
}

/**
 * Return selection string for current internal state.
 *
 * @see #getSelectionArgs()
 */
public String getSelection() {
    return mSelection.toString();
}

/**
 * Return selection arguments for current internal state.
 *
 * @see #getSelection()
 */
public String[] getSelectionArgs() {
    return mSelectionArgs.toArray(new String[mSelectionArgs.size()]);
}

private void mapColumns(String[] columns) {
    for (int i = 0; i < columns.length; i++) {
        final String target = mProjectionMap.get(columns[i]);
        if (target != null) {
            columns[i] = target;
        }
    }
}

@Override
public String toString() {
    return "SelectionBuilder[table=" + mTable + ", selection=" + getSelection()
            + ", selectionArgs=" + Arrays.toString(getSelectionArgs()) + "]";
}

/**
 * Execute query using the current internal state as {@code WHERE} clause.
 */
public Cursor query(SQLiteDatabase db, String[] columns, String orderBy) {
    return query(db, columns, null, null, orderBy, null);
}

/**
 * Execute query using the current internal state as {@code WHERE} clause.
 */
public Cursor query(SQLiteDatabase db, String[] columns, String groupBy,
                    String having, String orderBy, String limit) {
    assertTable();
    if (columns != null) mapColumns(columns);
    Log.v(TAG, "query(columns=" + Arrays.toString(columns) + ") " + this);
    return db.query(mTable, columns, getSelection(), getSelectionArgs(), groupBy, having,
            orderBy, limit);
}

/**
 * Execute distinct query using the current internal state as {@code WHERE} clause.
 */
public Cursor queryDistinct(SQLiteDatabase db, String[] columns, String orderBy) {
    return queryDistinct(db, columns, null, null, orderBy, null);
}
/**
 * Execute distinct query using the current internal state as {@code WHERE} clause.
 */
public Cursor queryDistinct(SQLiteDatabase db, String[] columns, String groupBy,
                    String having, String orderBy, String limit) {
    assertTable();
    if (columns != null) mapColumns(columns);
    Log.v(TAG, "queryDistinct(columns=" + Arrays.toString(columns) + ") " + this);
    return db.query(true, mTable, columns, getSelection(), getSelectionArgs(), groupBy, having,
            orderBy, limit);
}

/**
 * Execute update using the current internal state as {@code WHERE} clause.
 */
public int update(SQLiteDatabase db, ContentValues values) {
    assertTable();
    Log.v(TAG, "update() " + this);
    return db.update(mTable, values, getSelection(), getSelectionArgs());
}

/**
 * Execute delete using the current internal state as {@code WHERE} clause.
 */
public int delete(SQLiteDatabase db) {
    assertTable();
    Log.v(TAG, "delete() " + this);
    return db.delete(mTable, getSelection(), getSelectionArgs());
}
}

因此,您可以將這兩個幫助程序與ContentProvider結合使用,以創建自己的模型,並在其中定義使用這些幫助程序的查詢,插入,更新和刪除方法。 對我來說,最好的辦法是將Google Sync Framework與Loader結合使用。

http://developer.android.com/training/sync-adapters/index.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM