How to return a string from the method that SQLite queries?

1

How to make a method that returns a string with a single result?

I'm trying something like this:

public String getString(String var) {
    String selectQuery = 
           "SELECT * FROM "+TABLE_STATUS+" WHERE "+KEY_STATUS_KEY+"='"+var+"' LIMIT 1";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    //não sei como retornar a string
}
    
asked by anonymous 10.07.2014 / 17:26

1 answer

3

You can use the StringBuffer or the StringBuilder .

Here's an example:

public String getString(String var) 
{       
    String selectQuery = 
        "SELECT * FROM "+TABLE_STATUS+" WHERE "+KEY_STATUS_KEY+"='"+var+"' LIMIT 1";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    StringBuilder sb = new StringBuilder();

    cursor.moveToFirst();

    /*********** Fazer isto por cada coluna ***************/
    String nome_da_coluna_string = cursor.getString(cursor.getColumnIndex('nome_da_coluna'));

    sb.append(nome_da_coluna_string);
    /******************************************************/

    cursor.close();

    return sb.toString();
}
    
10.07.2014 / 17:39