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

Asked

Viewed 1,437 times

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
}

1 answer

3


You can use the Stringbuffer or the Stringbuilder.

Be 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.