2011年8月1日 星期一

SQLite query in android application

在Android 裡使用SQLite,最重要的class 就是 SQLiteOpenHelper


重點來了

getWritableDatabase()


Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate().




getReadableDatabase()


Like getWritableDatabase(), this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate().




說實在的,在對資料庫的抓取或異動還滿難用的....





public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Since: API Level 1
Query the given URL, returning a Cursor over the result set.
Parameters
distinct要不要 DISTINCT, TRUE/FALSE
table表格名稱
columnsnew String[]{"column01","column02","column03"};
selection "column01 = ? AND "column02 = ?";
selectionArgsnew String[]{"A","B","C"};

selection 與  selectionArgs 是配對的   
groupByA filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
havingA filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
orderByHow to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
limitLimits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause.
Returns
  • Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.


一般QUERY
SQLiteDatabase db = SQLiteOpenHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { "COLUMN01","COLUMN02" },
"cCarBocePartsIdn LIKE ?", new String[] { key+"%" }, null, null, null);

沒有留言: