我有一個應用程序從sqlite數據庫獲取所有數據並將其轉換爲JSON,但我想知道如果我只想獲取特定數據列?從SQLite數據庫檢索特定的列
這是我使用從SQLite的DATABSE得到的所有數據,並將其轉換成JSON代碼:
Cursor data = db.getCartItems();
orderNameArray = new JSONArray();
data.moveToFirst();
while(data.isAfterLast() == false)
{
int totalColumn = data.getColumnCount();
orderNameObject = new JSONObject();
for (int i = 0; i < totalColumn; i++)
{
try {
orderNameObject.put(data.getColumnName(i), data.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
orderNameArray.put(orderNameObject);
data.moveToNext();
}
data.close();
預先感謝所有的見解或提前幫助! :d
顯示你的db.getCartItems()你可以在你想要的特定列的查詢中進行更改 https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm – Pavan
哦,是啊哈哈。我是一個白癡,我可以創建另一個函數來獲得我需要的特定值。謝謝你,先生!您應得到Upvote :) –
如果您不想編寫自定義SELECT語句,還可以使用帶有「continue」的if語句跳過您選擇的列。 – Thracian