iT邦幫忙

DAY 2
1

Android開發~哎呀那些小麻煩系列 第 2

[Day2] Android開發的那些小麻煩 - 好用的Open Source Library GreenDAO

一邊趕code一邊還要PO文,真是夠嗆的XD

今天先COPY以前的舊文:P

這篇想要介紹greenDAO這個ORM Library,對於簡單的SQL來說非常好用,也可以執行一些grouy by跟巢狀查詢的指令,只要不太複雜的功能都可以簡單的實作出來。本篇預設你已經有在Android中撰寫SQL相關程式的經驗。

這系列的文章總共分三篇

  • 簡介
  • greenDAO的使用
  • 進階使用與心得

Android平台有提供一個library SQLiteOpenHelper可以讓developer簡單的(?)呼叫資料庫,根據developer.android.com當中提供的例子,用中文快速回復一下記憶

首先,要先定義你的table schema

public final class FeedReaderContract {
public FeedReaderContract() {}

/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
...
}
}

定義資料庫的創建(生)與刪除(死)的script

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
... // Any other options for the CREATE command
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;

然後,要建立一個DBHelper,來幫助管理資料庫的創建、刪除與升級(復活)

public class FeedReaderDbHelper extends SQLiteOpenHelper {

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

public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion,int newVersion){
onUpgrade(db, oldVersion, newVersion);
}
}

接著,要新增資料的時後code長這樣

SQLiteDatabase db = mDbHelper.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_CONTENT, content);

long newRowId;
newRowId = db.insert(
FeedEntry.TABLE_NAME,
FeedEntry.COLUMN_NAME_NULLABLE,
values);

要查詢資料的時候,code要寫這樣

SQLiteDatabase db = mDbHelper.getReadableDatabase();

String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_UPDATED,
...
};

String sortOrder =
FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);

刪除資料要寫這樣

// Define 'where' part of query.
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(rowId) };
// Issue SQL statement.
db.delete(table_name, selection, selectionArgs);

更新資料要寫這樣

SQLiteDatabase db = mDbHelper.getReadableDatabase();
// New value for one column
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
// Which row to update, based on the ID
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";
String[] selectionArgs = { String.valueOf(rowId) };
int count = db.update(
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);

而上面的每一步,如果忘記更新到一個欄位,或是多/少一個欄位要的時候,就要在project裡面到處找了。

如果今天developer要寫的code加起來只有這樣,那生活不是簡單多了嗎?

至於要怎麼開始使用呢? 讓我們繼續看下去.......或上官網自己查文件XD


上一篇
[Day1] Android開發的那些小麻煩 - 在開始之前
下一篇
Android開發的那些小麻煩- GreenDAO , 之前的準備工作(Step By Step)
系列文
Android開發~哎呀那些小麻煩5

尚未有邦友留言

立即登入留言