這個問題在這里已經有了答案: 在資料庫 SQLite 中過濾查詢 3 個答案 從游標中過濾掉專案 2 個答案 昨天關門。
假設我有四個屬性的表:id、name、address 和電話號碼。假設創建了一個單獨的資料庫游標類來獲取表中學生的所有記錄,我如何使用這個游標來獲取只有特定 id 的記錄或按 id 對記錄進行排序?提前致謝!
uj5u.com熱心網友回復:
典型的方法是使用查詢,它驅動游標的構建來呼叫 SELECT SQL,該 SQL 通過 WHERE 子句選擇所需的行,并且在排序的情況下使用 ORDER BY 子句。
Cursor 只是一個靈活的類,旨在保存此類查詢的任何輸出。
例如,您可以使用querySQLiteDatabase 類提供的便捷方法:-
Long id=0L; /* set according to the id required */
Cursor csr = db.query(
"the_table", /* the name of the table */
null, /* null equates to * which means all unhidden columns */
"id=?", /* the WHERE clause without the WHERE keyword */
new String[]{String.valueOf(id)}, /* A String array of the arguments 1 arg for each ? place holder */
null, /* the GROUP BY clause without the GROUP BY keyword */
null, /* the HAVING clause without the HAVING keyword */
"id ASC" /* the ORDER BY clause without the ORDER BY keyword */
);
/* This would build and execute a query that equates to"-
SELECT * FROM the_table WHERE id='n' ORDER BY id ASC
where n would be the value as per the variable id (0)
*/
/* The cursor will contain the respective rows in the respective order */
while (csr.moveToNext()) {
/* do something with the data held in the Cursor */
}
csr.close(); /* should always close the cursor when done with it */
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510173.html
上一篇:在資料庫上執行SQL查詢時出錯:濫用聚合:count()
下一篇:如何插入strftime的輸出?
