Bu dersimizde veritabanına yazdığımız verileri okumayı öğreneceğiz. Bu işlem için database helper ile veritabanımızı açarız.
Derslerimizde genel olarak projection değişkeni veritabanından okumak istediğimiz alanları, selection koşul ifadelerindeki sütun adını, selectionArg ise bu sütunun değerini temsil edecek.
Daha sonra query methodunu ilgili parametreler ile çağırırız ve bu methodu geriye istenilen verileri içeren bir cursor nesnesi döndürür.
Daha sonra bir while döngüsüyle bu cursor’ın içeriğini satır satır okuruz.
Bu derste yazılan method aşağıdaki gibidir:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
private void notlariOku() { DatabaseHelper helper = new DatabaseHelper(this); SQLiteDatabase db = helper.getReadableDatabase(); String[] projection = {NotlarEntry._ID, NotlarEntry.COLUMN_NOT_ICERIK, NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, NotlarEntry.COLUMN_BITIS_TARIHI, NotlarEntry.COLUMN_YAPILDI, NotlarEntry.COLUMN_KATEGORI_ID}; String selection = NotlarEntry.COLUMN_KATEGORI_ID + " = ?"; String[] selectionArgs = {"1"}; Cursor c = db.query(NotlarEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null); int i = c.getCount(); Toast.makeText(this, "Satır sayısı=" + i, Toast.LENGTH_LONG).show(); String tumNotlar = ""; while (c.moveToNext()) { for (int j = 0; j <= 5; j++) { tumNotlar += c.getString(j) + " - "; } tumNotlar += "\n"; } Log.e("SONUC", tumNotlar); c.close(); db.close(); } |