Convenience method to return a single Long from SQLiteDatabase. - Android Database

Android examples for Database:SQL Query

Description

Convenience method to return a single Long from SQLiteDatabase.

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{
    /**/*from   w ww.j ava2 s.co m*/
     * Convenience method to return a single {@link Long}.
     *
     * @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 single {@link Long} result. Set to {@code null} if an error occurred.
     */
    public static Long safeQueryForLong(SQLiteDatabase db,
            String tableName, String column, String selection,
            String[] selectionArgs) {
        return safeQueryForLong(db, tableName, column, selection,
                selectionArgs, null, null, null);
    }
    /**
     * Convenience method to return a single {@link Long}.
     *
     * @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 single {@link Long} result. Set to {@code null} if an error occurred.
     */
    public static Long safeQueryForLong(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.getLong(0) : null;
        } catch (SQLiteException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}

Related Tutorials