Example usage for android.database.sqlite SQLiteDatabase rawQuery

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

Introduction

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

Prototype

public Cursor rawQuery(String sql, String[] selectionArgs) 

Source Link

Document

Runs the provided SQL and returns a Cursor over the result set.

Usage

From source file:org.nuxeo.android.cache.sql.DeferedUpdateTableWrapper.java

public List<CachedOperationRequest> getPendingRequests(Session session) {
    SQLiteDatabase db = getReadableDatabase();

    String sql = "select * from " + getTableName();
    Cursor cursor = db.rawQuery(sql, null);

    List<CachedOperationRequest> result = new ArrayList<CachedOperationRequest>();

    try {/*from  w  ww.j a  va 2s.  co m*/
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {

            do {
                String operationKey = cursor.getString(cursor.getColumnIndex(KEY_COLUMN));
                String operationId = cursor.getString(cursor.getColumnIndex(OPID_COLUMN));
                OperationType opType = OperationType
                        .fromString(cursor.getString(cursor.getColumnIndex(OPTYPE_COLUMN)));
                String jsonParams = cursor.getString(cursor.getColumnIndex(PARAMS_COLUMN));
                String jsonHeaders = cursor.getString(cursor.getColumnIndex(HEADERS_COLUMN));
                String jsonCtx = cursor.getString(cursor.getColumnIndex(CTX_COLUMN));
                String inputType = cursor.getString(cursor.getColumnIndex(INPUT_TYPE_COLUMN));
                String inputRef = cursor.getString(cursor.getColumnIndex(INPUT_REF_COLUMN));
                String deps = cursor.getString(cursor.getColumnIndex(DEPS_COLUMN));
                Boolean inputBin = false;
                if (inputType != null) {
                    inputBin = new Boolean(cursor.getString(cursor.getColumnIndex(INPUT_BINARY_COLUMN)));
                }

                /*
                 * OperationDocumentation op =
                 * session.getOperation(operationId);
                 * Map<String, String> params =
                 * JSONHelper.readMapFromJson(jsonParams);
                 * Map<String, String> headers =
                 * JSONHelper.readMapFromJson(jsonHeaders);
                 * Map<String, String> ctx =
                 * JSONHelper.readMapFromJson(jsonCtx);
                 * OperationInput input = null;
                 * if
                 * (!cursor.isNull(cursor.getColumnIndex(INPUT_TYPE_COLUMN
                 * ))) {
                 * final String inputType =
                 * cursor.getString(cursor.getColumnIndex
                 * (INPUT_TYPE_COLUMN));
                 * final String inputRef =
                 * cursor.getString(cursor.getColumnIndex
                 * (INPUT_REF_COLUMN));
                 * Boolean inputBin = new
                 * Boolean(cursor.getString(cursor.getColumnIndex
                 * (INPUT_BINARY_COLUMN)));
                 * if (inputBin) {
                 * input = new FileBlob(null);
                 * // XX read Binary here
                 * } else {
                 * input = new OperationInput() {
                 * @Override
                 * public boolean isBinary() {
                 * return false;
                 * }
                 * @Override
                 * public String getInputType() {
                 * return inputType;
                 * }
                 * @Override
                 * public String getInputRef() {
                 * return inputRef;
                 * }
                 * };
                 * }
                 * }
                 * OperationRequest deferredRequest = new
                 * DefaultOperationRequest((DefaultSession) session,op,
                 * params, headers, ctx,input);
                 */
                OperationRequest deferredRequest = OperationPersisterHelper.rebuildOperation(session,
                        operationId, jsonParams, jsonHeaders, jsonCtx, inputType, inputRef, inputBin);
                if (deps != null) {
                    deferredRequest.getDependencies().merge(ExecutionDependencies.fromJSON(deps));
                }
                result.add(new CachedOperationRequest(deferredRequest, operationKey, opType));

            } while (cursor.moveToNext());
        }
        return result;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

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

/**
 * Reads the database for all chatrooms.
 * @return A list of all chatrooms./*from  ww  w.ja v  a  2  s. co m*/
 */
public List<Chatroom> getAllChatrooms() {
    List<Chatroom> chatroomList = new ArrayList<Chatroom>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CHATROOMS;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Chatroom chatroom = new Chatroom();
            chatroom.setName(cursor.getString(0));
            chatroom.setLatitude(cursor.getDouble(2));
            chatroom.setLongitude(cursor.getDouble(StringLiterals.THREE));
            chatroom.setRadius(cursor.getFloat(StringLiterals.FOUR));
            // Adding chatroom to list
            chatroomList.add(chatroom);
        } while (cursor.moveToNext());
    }

    // return contact list
    return chatroomList;
}

From source file:com.clutch.ClutchStats.java

public ArrayList<ABRow> getABLogs() {
    SQLiteDatabase db = getReadableDatabase();
    String[] args = {};/*from   w ww. j a  v a  2  s .c  o m*/
    ArrayList<ABRow> res = new ArrayList<ABRow>();
    Cursor cur = db.rawQuery("SELECT uuid, ts, data FROM ablog ORDER BY ts", args);
    cur.moveToFirst();
    while (!cur.isAfterLast()) {
        String uuid = cur.getString(0);
        double ts = cur.getDouble(1);
        JSONObject data;
        try {
            data = new JSONObject(cur.getString(2));
        } catch (JSONException e) {
            Log.w(TAG, "Could not serialize to JSON: " + cur.getString(3));
            cur.moveToNext();
            continue;
        }
        res.add(new ABRow(uuid, ts, data));
        cur.moveToNext();
    }
    db.close();
    return res;
}

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

public Wallpaper getRandomWallpaper() {
    Wallpaper wallpaper = null;/*  w  w  w . j av  a2 s.  c  o  m*/
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_WALLPAPERS + " ORDER BY RANDOM() LIMIT 1";
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        do {
            wallpaper = new Wallpaper(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpaper;
}

From source file:com.clutch.ClutchStats.java

public ArrayList<StatRow> getLogs() {
    SQLiteDatabase db = getReadableDatabase();
    String[] args = {};//from  w  w w  .  j ava 2  s  . c  o  m
    ArrayList<StatRow> res = new ArrayList<StatRow>();
    Cursor cur = db.rawQuery("SELECT uuid, ts, action, data FROM stats ORDER BY ts", args);
    cur.moveToFirst();
    while (!cur.isAfterLast()) {
        String uuid = cur.getString(0);
        double ts = cur.getDouble(1);
        String action = cur.getString(2);
        JSONObject data;
        try {
            data = new JSONObject(cur.getString(3));
        } catch (JSONException e) {
            Log.w(TAG, "Could not serialize to JSON: " + cur.getString(3));
            cur.moveToNext();
            continue;
        }
        res.add(new StatRow(uuid, ts, action, data));
        cur.moveToNext();
    }
    db.close();
    return res;
}

From source file:de.tu_berlin.snet.commstat.SensorFragment.java

private void reloadStatus() {
    if (pipeline == null || !pipeline.isEnabled() || getActivity() == null)
        return;//from   w ww . j  a va 2 s.com

    try {
        String TOTAL_COUNT_SQL = "SELECT count(*) FROM " + NameValueDatabaseHelper.DATA_TABLE.name;
        String EMAIL_COUNT_SQL = TOTAL_COUNT_SQL + " WHERE name LIKE '%EmailProbe' ";
        String FACEBOOK_COUNT_SQL = TOTAL_COUNT_SQL + " WHERE name LIKE '%FacebookProbe' ";

        // Query the pipeline db for the count of rows in the data table
        SQLiteDatabase db = pipeline.getDb();
        Cursor mcursor = db.rawQuery(TOTAL_COUNT_SQL, null);
        mcursor.moveToFirst();
        final int totalCount = mcursor.getInt(0);

        mcursor = db.rawQuery(EMAIL_COUNT_SQL, null);
        mcursor.moveToFirst();
        final int emailCount = mcursor.getInt(0);

        mcursor = db.rawQuery(FACEBOOK_COUNT_SQL, null);
        mcursor.moveToFirst();
        final int fbCount = mcursor.getInt(0);

        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                statusTextView.setText(
                        Html.fromHtml("<b> Archive Size: </b>" + String.format("%1$,.2f", getArchiveSize())
                                + " MB" + "<br><b>Total Database Entries: </b>" + String.valueOf(totalCount)
                                + "<br><b>Email Database Entries: </b>" + String.valueOf(emailCount)
                                + "<br><b>Facebook Database Entries: </b>" + String.valueOf(fbCount)));
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ru.gkpromtech.exhibition.db.DbHelper.java

public String getPragma(SQLiteDatabase db, String pragma) {
    String value = null;//ww w . jav a 2  s .  c  om
    Cursor cursor = db.rawQuery("PRAGMA " + pragma, null);
    if (cursor.moveToNext())
        value = cursor.getString(0);
    cursor.close();
    return value;
}

From source file:com.shalzz.attendance.DatabaseHandler.java

/**
 * Checks for any obsolete data, based on the timestamp,
 * and deletes if any./*from  www . ja  va  2  s.c  o m*/
 * @return 1 if one or more subjects are purged else 0
 */
public int purgeOldSubjects() {
    SQLiteDatabase db = getWritableDatabase();
    try (Cursor cursor = db.rawQuery(Subject.DELETE_OBSOLETE, null)) {
        int count = cursor.getCount();
        cursor.close();
        return count;
    }
}

From source file:com.shalzz.attendance.DatabaseHandler.java

/**
 * Checks for any obsolete data, based on the timestamp,
 * and deletes if any.// w  ww.  j ava 2s. c o m
 * @return 1 if one or more Periods are purged else 0
 */
public int purgeOldPeriods() {
    SQLiteDatabase db = getWritableDatabase();
    try (Cursor cursor = db.rawQuery(Period.DELETE_OBSOLETE, null)) {
        int count = cursor.getCount();
        cursor.close();
        return count;
    }
}

From source file:com.shalzz.attendance.DatabaseHandler.java

/**
 * Check if the attendance data is in database.
 * *///  w  w  w .  j  a v a 2 s  . c o m
public int getSubjectCount() {
    SQLiteDatabase db = getReadableDatabase();
    try (Cursor cursor = db.rawQuery(Subject.SELECT_COUNT, null)) {
        int rowCount = 0;
        if (cursor.moveToNext())
            rowCount = cursor.getInt(0);

        return rowCount;
    }
}