Example usage for android.database.sqlite SQLiteDatabase query

List of usage examples for android.database.sqlite SQLiteDatabase query

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase query.

Prototype

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
        String having, String orderBy, String limit) 

Source Link

Document

Query the given table, returning a Cursor over the result set.

Usage

From source file:org.frc836.database.DB.java

public static String getConfigNameFromID(int config, SQLiteDatabase db) {

    String[] projection = { CONFIGURATION_LU_Entry.COLUMN_NAME_CONFIGURATION_DESC };
    String[] where = { String.valueOf(config) };
    Cursor c = db.query(CONFIGURATION_LU_Entry.TABLE_NAME, projection, // select
            CONFIGURATION_LU_Entry.COLUMN_NAME_ID + "= ?", where, // EventName
            null, // don't group
            null, // don't filter
            null, // don't order
            "0,1"); // limit to 1
    String ret = "";
    try {// w  w  w  . ja va  2  s  .  c om
        c.moveToFirst();
        ret = c.getString(c.getColumnIndexOrThrow(CONFIGURATION_LU_Entry.COLUMN_NAME_CONFIGURATION_DESC));
    } finally {
        if (c != null)
            c.close();
    }
    return ret;
}

From source file:org.egov.android.data.SQLiteHelper.java

public Cursor query(String tableName, String[] columns, String selection, String[] selectionArgs,
        String groupBy, String having, String orderBy, String limit) {

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

    return cursor;
}

From source file:org.egov.android.data.SQLiteHelper.java

/**
 * Get total records in particular table
 * /*from w  ww. j a  va2  s. co  m*/
 * @param table
 * @param selection
 * @param selectionArgs
 * @param limit
 * @return int
 */

public int getRecordCount(String table, String selection, String[] selectionArgs, String limit) {
    try {
        String[] columns = { " count(*) as totalRows" };
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(table, columns, selection, selectionArgs, null, null, null, limit);
        cursor.moveToFirst();
        int rows = cursor.getInt(0);
        cursor.close();
        db.close();
        return rows;
    } catch (Exception ex) {
        ex.printStackTrace();
        return 0;
    }
}

From source file:com.dm.material.dashboard.candybar.databases.Database.java

public boolean isWallpapersEmpty() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, null, null);
    int rowCount = cursor.getCount();
    cursor.close();//from   ww  w .j a v a2 s . com
    db.close();
    return rowCount == 0;
}

From source file:com.dm.material.dashboard.candybar.databases.Database.java

public boolean isRequested(String activity) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_REQUEST, null, KEY_ACTIVITY + " = ?", new String[] { activity }, null, null,
            null, null);//ww  w.j a  va 2s .c  om
    int rowCount = cursor.getCount();
    cursor.close();
    db.close();
    return rowCount > 0;
}

From source file:com.cuddlesoft.nori.database.APISettingsDatabase.java

/**
 * Get single instance of {@link SearchClient.Settings} from the database.
 *
 * @param id Row ID./*  w ww .j  a v  a  2 s  . c o m*/
 * @return Search client settings object. Null if given ID does not exist in the database.
 */
public SearchClient.Settings get(long id) {
    // Query the database.
    SQLiteDatabase db = getReadableDatabase();
    Cursor c = db.query(TABLE_NAME, null, COLUMN_ID + " = ?", new String[] { Long.toString(id) }, null, null,
            COLUMN_ID, "1");

    // Prepare return value.
    final SearchClient.Settings settings = (!c.moveToNext()) ? null : cursorToSearchClientSettings(c);

    // Clean up native resources.
    c.close();
    db.close();

    return settings;
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

public Course getCourse(String classNmbr) {
    Course course = new Course();
    try {/*from   w  w  w.j a  v  a  2 s .com*/
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.query(TABLE_COURSES, COLUMNS, KEY_CLASNBR + "=?",
                new String[] { String.valueOf(classNmbr) }, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        course.setCLASS_NUMBER(cursor.getString(0));
        course.setCOURSE_TITLE(cursor.getString(1));
        course.setCOURSE_SLOT(cursor.getString(2));
        course.setCOURSE_TYPE(cursor.getString(3));
        course.setCOURSE_TYPE_SHORT(cursor.getString(4));
        course.setCOURSE_LTPC(cursor.getString(5));
        course.setCOURSE_CODE(cursor.getString(6));
        course.setCOURSE_MODE(cursor.getString(7));
        course.setCOURSE_OPTION(cursor.getString(8));
        course.setCOURSE_VENUE(cursor.getString(9));
        course.setCOURSE_FACULTY(cursor.getString(10));
        course.setCOURSE_REGISTRATIONSTATUS(cursor.getString(11));
        course.setCOURSE_BILL_DATE(cursor.getString(12));
        course.setCOURSE_BILL_NUMBER(cursor.getString(13));
        course.setCOURSE_PROJECT_TITLE(cursor.getString(14));
        course.setJson(new JSONObject(cursor.getString(15)));
        course.setCOURSE_ATTENDANCE(ParseCourses.getAttendance(new JSONObject(cursor.getString(16))));
        course.setCOURSE_TIMING(ParseCourses.getTimings(new JSONArray(cursor.getString(17))));
        course.setCOURSE_MARKS(ParseCourses.getCouseMarks(new JSONObject(cursor.getString(18))));

        cursor.close();
        db.close();

    } catch (Exception e) {
        e.printStackTrace();
        SQLiteDatabase _db = this.getWritableDatabase();
        if (_db != null && _db.isOpen()) {
            _db.close();
        }
    }

    return course;
}

From source file:com.example.shutapp.DatabaseHandler.java

/**
 * Reads the database for a chatroom.//from  w  w  w .jav  a  2  s . co  m
 * @param name of the chatroom
 * @return the chatroom object
 */
public Chatroom getChatroom(String name) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CHATROOMS,
            new String[] { KEY_NAME, KEY_LATITUDE, KEY_LONGITUDE, KEY_RADIUS }, KEY_NAME + "=?",
            new String[] { name }, null, null, null, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }

    Chatroom chatroom = new Chatroom(cursor.getString(0), cursor.getDouble(1), cursor.getDouble(2),
            cursor.getFloat(StringLiterals.THREE));
    // return chatroom
    return chatroom;
}

From source file:com.dm.wallpaper.board.databases.Database.java

public int getWallpapersCount() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, null, null);
    int rowCount = cursor.getCount();
    cursor.close();/*from  w  w w .j a  v  a 2 s  . com*/
    db.close();
    return rowCount;
}

From source file:com.dm.wallpaper.board.databases.Database.java

@Nullable
public Wallpaper getRandomWallpaper() {
    Wallpaper wallpaper = null;//from  w w  w  . ja v  a  2  s. c o m
    List<String> selected = getSelectedCategories(true);
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return null;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {
            CONDITION.append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        }
        selection.add("%" + item.toLowerCase(Locale.getDefault()) + "%");
    }

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, "RANDOM()", "1");
    if (cursor.moveToFirst()) {
        do {
            wallpaper = new Wallpaper(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4), cursor.getString(5));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpaper;
}