samedi 28 février 2015

SQLite RawQuery Android

sorry for the noob question.


But I am little stuck as to how to continue with this Method I am writing for an Android Application.


I wish to select some data from two columns in a database based on whether or not two other columns meet a certain condition. I tried creating a raw query but think that I am doing it completely wrong.


Below is the Method I wrote:



public String[] getDayTotals(String year, String month) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();

String query = "SELECT " + sqlHelper.DAY_TOTAL + " AND " + sqlHelper.DAY + " FROM " + sqlHelper.TABLE_NAME + " WHERE " + sqlHelper.YEAR + "=? AND " + sqlHelper.MONTH + "=?";
String[] args = {year, month};

Cursor cursor = db.rawQuery(query, args);
cursor.moveToFirst();

StringBuilder sb = new StringBuilder();

while(cursor.moveToNext()) {

int indexTotal = cursor.getColumnIndex(sqlHelper.DAY_TOTAL);
int indexDay = cursor.getColumnIndex(sqlHelper.DAY);

String value = cursor.getString(indexTotal);
String day = cursor.getString(indexDay);

sb.append(value + "," + day + "\n");

}

String[] temp = sb.toString().split("\n");

return temp;
}


I am very new to SQL and as such am not quite sure how to continue. I do apologize for any stupid mistakes I have made.


Aucun commentaire:

Enregistrer un commentaire