Returns a single String from a query from Database. - Android Database

Android examples for Database:Table Row Query

Description

Returns a single String from a query from Database.

Demo Code


import java.util.ArrayList;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class Main{
    /**/*  w  w  w  .  ja  v a 2 s .  co  m*/
     * Returns a single {@code String} from a query.
     *
     * @param db The database to query.
     * @param tableName The table name to compile the query against.
     * @param column The column to return.
     * @param selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will
     * return all rows for the given table.
     * @param selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the
     * selection. The values will be bound as Strings.
     * @return The String representation of the result. Set to {@code null} if an error occurred.
     */
    public static String safeQueryForString(SQLiteDatabase db,
            String tableName, String column, String selection,
            String[] selectionArgs) {
        return safeQueryForString(db, tableName, column, selection,
                selectionArgs, null, null, null);
    }
    /**
     * Returns a single {@code String}.
     *
     * @param db The database to query.
     * @param tableName The table name to compile the query against.
     * @param column The column to return.
     * @param selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will
     * return all rows for the given table.
     * @param selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the
     * selection. The values will be bound as Strings.
     * @param groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will
     * cause the rows to not be grouped.
     * @param having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause
     * (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
     * @param orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default
     * sort order, which may be unordered.
     * @return The String representation of the result. Set to {@code null} if an error occurred.
     */
    public static String safeQueryForString(SQLiteDatabase db,
            String tableName, String column, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy) {
        Cursor cursor = db.query(tableName, new String[] { column },
                selection, selectionArgs, groupBy, having, orderBy, "1");
        try {
            return cursor.moveToFirst() ? cursor.getString(0) : null;
        } catch (SQLiteException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}

Related Tutorials