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:github.popeen.dsub.util.SongDBHandler.java

public synchronized Pair<Integer, String> getIdFromPath(String path) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] columns = { SONGS_SERVER_KEY, SONGS_SERVER_ID };
    Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null,
            null, SONGS_LAST_PLAYED + " DESC", null);

    try {/*from w w  w. ja v a 2s . c o  m*/
        cursor.moveToFirst();
        return new Pair(cursor.getInt(0), cursor.getString(1));
    } catch (Exception e) {
        return null;
    } finally {
        db.close();
    }
}

From source file:github.popeen.dsub.util.SongDBHandler.java

public synchronized Pair<Integer, String> getIdFromPath(int serverKey, String path) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] columns = { SONGS_SERVER_KEY, SONGS_SERVER_ID };
    Cursor cursor = db.query(TABLE_SONGS, columns,
            SONGS_SERVER_KEY + " = ? AND " + SONGS_COMPLETE_PATH + " = ?",
            new String[] { Integer.toString(serverKey), path }, null, null, null, null);

    try {/*from   w  w w  .  jav  a 2s  .  c o  m*/
        cursor.moveToFirst();
        return new Pair(cursor.getInt(0), cursor.getString(1));
    } catch (Exception e) {
        return null;
    } finally {
        db.close();
    }
}

From source file:github.popeen.dsub.util.SongDBHandler.java

public synchronized Long[] getLastPlayed(int serverKey, String id) {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] columns = { SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED };
    Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?",
            new String[] { Integer.toString(serverKey), id }, null, null, null, null);

    try {//from w  w  w . ja va  2s . c o m
        cursor.moveToFirst();

        Long[] dates = new Long[2];
        dates[0] = cursor.getLong(0);
        dates[1] = cursor.getLong(1);
        return dates;
    } catch (Exception e) {
        return null;
    } finally {
        db.close();
    }
}

From source file:com.rs.TCOfflineStatementCollection.java

/**
 * get all statements that haven't been posted
 * @param limit// ww  w . j  av  a  2s . c o m
 * @return List of LocalStatementsItem
 */
List<LocalStatementsItem> getUnsentStatements(int limit) {
    List<LocalStatementsItem> statementArray = new ArrayList<LocalStatementsItem>();

    Cursor cursor;
    SQLiteDatabase database;
    TCLocalStorageDatabaseOpenHelper dbHelper;
    dbHelper = new TCLocalStorageDatabaseOpenHelper(appContext);
    database = dbHelper.getWritableDatabase();

    String select = LocalStatements.POSTED_DATE + "=" + "\'" + "0" + "\'";

    cursor = database.query(TCOfflineDataManager.LOCAL_STATEMENT_TABLE_NAME, null, select, null, null, null,
            LocalStatements.CREATE_DATE + " ASC", Integer.toString(limit)); //query for all the unposted statements

    cursor.moveToFirst(); //go to the beginning of the query and then loop through all the packages, adding them to the return List
    while (!cursor.isAfterLast()) {
        LocalStatementsItem thisPackage = new LocalStatementsItem();
        thisPackage.id = cursor.getInt(0);
        thisPackage.statementId = cursor.getString(cursor.getColumnIndex("statementId"));
        thisPackage.statementJson = cursor.getString(cursor.getColumnIndex("statementJson"));
        thisPackage.createDate = cursor.getLong(cursor.getColumnIndex("createDate"));
        thisPackage.postedDate = cursor.getLong(cursor.getColumnIndex("postedDate"));
        thisPackage.querystring = cursor.getString(cursor.getColumnIndex("querystring"));

        statementArray.add(thisPackage);
        cursor.moveToNext();
    }

    cursor.close();
    database.close();

    return statementArray;
}

From source file:github.popeen.dsub.util.SongDBHandler.java

public JSONArray exportData() {
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = { SONGS_ID, SONGS_SERVER_KEY, SONGS_SERVER_ID, SONGS_COMPLETE_PATH, SONGS_LAST_PLAYED,
            SONGS_LAST_COMPLETED };/* w w w.  java  2s.c  o m*/
    Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_LAST_PLAYED + " != ''", null, null, null, null, null);
    try {
        JSONArray jsonSongDb = new JSONArray();
        while (cursor.moveToNext()) {
            JSONObject tempJson = new JSONObject();
            tempJson.put("SONGS_ID", cursor.getInt(0));
            tempJson.put("SONGS_SERVER_KEY", cursor.getInt(1));
            tempJson.put("SONGS_SERVER_ID", cursor.getString(2));
            tempJson.put("SONGS_COMPLETE_PATH", cursor.getString(3));
            tempJson.put("SONGS_LAST_PLAYED", cursor.getInt(4));
            tempJson.put("SONGS_LAST_COMPLETED", cursor.getInt(5));
            jsonSongDb.put(tempJson);
        }
        cursor.close();
        return jsonSongDb;
    } catch (Exception e) {
    }
    return new JSONArray();
}

From source file:pl.reticular.ttw.HighScoresActivity.java

private Cursor createCursor() {
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    String table = ResultsTableHelper.TABLE_NAME;

    String[] columns = new String[] { ResultsTableHelper.RESULT._ID, ResultsTableHelper.RESULT.DATE,
            ResultsTableHelper.RESULT.SCORE };

    String orderBy = ResultsTableHelper.RESULT.SCORE + " DESC, " + ResultsTableHelper.RESULT.DATE + " DESC";

    return db.query(table, columns, null, null, null, null, orderBy, null);
}

From source file:com.boardgamegeek.util.SelectionBuilder.java

/**
 * Execute query using the current internal state as {@code WHERE} clause.
 *//*from w  w w.j  a v  a  2  s.  c  om*/
public Cursor query(SQLiteDatabase db, String[] columns, String groupBy, String having, String orderBy,
        String limit) {
    assertTable();
    if (columns != null) {
        mapColumns(columns);
    }
    Timber.v("QUERY: columns=" + Arrays.toString(columns) + ", " + this);
    Cursor c = db.query(mTable, columns, getSelection(), getSelectionArgs(), groupBy, having, orderBy, limit);
    Timber.v("queried " + c.getCount() + " rows");
    return c;
}

From source file:com.nonninz.robomodel.RoboModel.java

void loadRecord(int position) throws InstanceNotFoundException {
    // Retrieve current entry in the database
    SQLiteDatabase db = mDatabaseManager.openOrCreateDatabase(getDatabaseName());
    Cursor query;// w  w  w .j  a v  a2  s  . co m

    final String limit = String.format("%d,1", position);
    try {
        query = db.query(getTableName(), null, null, null, null, null, _ID, limit);
    } catch (final SQLiteException e) {
        mDatabaseManager.createOrPopulateTable(mTableName, getSavedFields(), db);
        query = db.query(getTableName(), null, null, null, null, null, _ID, limit);
    }

    if (query.moveToFirst()) {
        try {
            setFieldsWithQueryResult(query);
        } catch (DatabaseNotUpToDateException e) {
            Ln.w(e, "Updating table %s", mTableName);
            query.close();

            // Update table with new columns
            mDatabaseManager.createOrPopulateTable(mTableName, getSavedFields(), db);
            mDatabaseManager.closeDatabase();
            db = mDatabaseManager.openOrCreateDatabase(getDatabaseName());

            // Retry
            try {
                query = db.query(getTableName(), null, null, null, null, null, _ID, limit);
                query.moveToFirst();
                setFieldsWithQueryResult(query);
            } catch (DatabaseNotUpToDateException ee) {
                throw new RuntimeException("Could not repair database.", ee);
            }
        }
        // set ID
        mId = query.getLong(query.getColumnIndex(_ID));

        query.close();
    } else {
        query.close();
        final String msg = String.format("No entry in database in position %d for model %s", position,
                getTableName());
        throw new InstanceNotFoundException(msg);
    }
}

From source file:com.android.leanlauncher.WidgetPreviewLoader.java

private Bitmap readFromDb(String name, Bitmap b) {
    if (mCachedSelectQuery == null) {
        mCachedSelectQuery = CacheDb.COLUMN_NAME + " = ? AND " + CacheDb.COLUMN_SIZE + " = ?";
    }/*w  ww.  j  av  a  2s. com*/
    SQLiteDatabase db = mDb.getReadableDatabase();
    Cursor result;
    try {
        result = db.query(CacheDb.TABLE_NAME, new String[] { CacheDb.COLUMN_PREVIEW_BITMAP }, // cols to return
                mCachedSelectQuery, // select query
                new String[] { name, mSize }, // args to select query
                null, null, null, null);
    } catch (SQLiteDiskIOException e) {
        recreateDb();
        return null;
    } catch (SQLiteCantOpenDatabaseException e) {
        throw e;
    }
    if (result.getCount() > 0) {
        result.moveToFirst();
        byte[] blob = result.getBlob(0);
        result.close();
        final BitmapFactory.Options opts = mCachedBitmapFactoryOptions.get();
        opts.inBitmap = b;
        opts.inSampleSize = 1;
        try {
            return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts);
        } catch (IllegalArgumentException e) {
            removeItemFromDb(mDb, name);
            return null;
        }
    } else {
        result.close();
        return null;
    }
}

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

@Nullable
public int getWallpapersCountOfCatgegory(String category) {
    List<String> selection = new ArrayList<>();

    StringBuilder CONDITION = new StringBuilder();
    CONDITION.append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
    selection.add("%" + category.toLowerCase(Locale.getDefault()) + "%");

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, null, null);
    int numWallpapers = 0;
    if (cursor.moveToFirst()) {
        do {/* w  w  w  .  j  a  v a  2  s  . c  o m*/
            numWallpapers++;
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return numWallpapers;
}