....

2014年5月29日 星期四

[長知識] Android 利用SQLite語法找出兩個日期內的資料 (SQLite Manager)

再查詢資料庫前
我們必須要有資料庫存在
而這邊我也有存了資料進去
最後才能進行語法搜尋


首先
要能夠看到資料庫裏面的欄位以及資料
我是使用Firefox的外掛程式 --> SQLite Manager
點選設定裡面的外掛程式 搜尋 即可以下載



再匯入我們的SQLite


阿~~~~
確定有資料了之後
重點開始~~~~~~~~~~~
可以開始在Android下語法搜尋資料了~~(灑淚)



//一開始當然要先new一個DatabaseHelper

InsertLocHlp insertLocHlp = new InsertLocHlp(MapV2.this, InsertLocHlp.DATABASES_NAME, null, InsertLocHlp.version);


//要讓資料庫能夠讀取

SQLiteDatabase db = insertLocHlp.getReadableDatabase();


//進行搜尋,這邊我是要搜尋time2時間內的latitude和longitude,利用cursor
//重要的是紅色的地方,在time2>=?以及time2<=?時,而? 是指 whereArg 
//dateFrom, dateTo是我從textView撈出來的日期

String[] whereArg = new String[]{dateFrom, dateTo};
Cursor  cursor = db.query(insertLocHlp.TABLE_NAME, new String[]{"latitude, longitude, time2"}, "time2>=?" + "And time2<=?", whereArg, null, null, null);


//利用cursor.moveToNext(),將全部的值都列印出來
//cursor.getString(0)對應到的陣列是latitude
//cursor.getString(1)對應到的陣列是lngitude

while (cursor.moveToNext()) {
value += cursor.getString(0) +","+ cursor.getString(1) + "\n";
}


//記得如果用完了資料庫和cursor一定要記得關掉,不然有可能會編譯錯誤哦 :)

cursor.close();
db.close();


//最後就完成了資料庫的搜尋了且取出條件內的欄位~OHYA



沒有留言:

張貼留言