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:com.maxwen.wallpaper.board.databases.Database.java

@Nullable
public List<Wallpaper> getWallpapersOfCatgegory(String category) {
    List<Wallpaper> wallpapers = new ArrayList<>();
    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);
    if (cursor.moveToFirst()) {
        do {//from   www  .  ja  v  a2  s .c om
            Wallpaper wallpaper = new Wallpaper(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getInt(6) == 1,
                    cursor.getLong(7));
            wallpapers.add(wallpaper);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

public List<T> select(String selection, String[] selectionArgs, String groupBy, String having, String orderBy,
        String limit) {//from w  ww.  j av a2  s.c  o  m
    List<T> result = new ArrayList<>();

    SQLiteDatabase db = mSqlHelper.getReadableDatabase();
    Cursor cursor = db.query(mTableName, mColumns, selection, selectionArgs, groupBy, having, orderBy, limit);
    //noinspection TryFinallyCanBeTryWithResources
    try {
        while (cursor.moveToNext()) {
            T entity = mEntityClass.newInstance();
            for (int i = 0; i < mFields.length; ++i)
                fillFieldValue(mType[i], mFields[i], entity, cursor, i);
            result.add(entity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
        db.close();
    }

    return result;
}

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

@Nullable
public List<Object> getWallpapersOfCatgegoryUnified(String category) {
    List<Object> wallpapers = new ArrayList<>();
    List<String> selection = new ArrayList<>();
    Map<String, Category> categoryMap = getCategoryMap();

    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);
    if (cursor.moveToFirst()) {
        Category c = new Category(0, category, categoryMap.get(category).getThumbUrl(), false);
        wallpapers.add(c);//  w ww.  j ava  2s.c o m
        int numWallpapers = 0;
        do {
            Wallpaper wallpaper = new Wallpaper(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getInt(6) == 1,
                    cursor.getLong(7));
            wallpapers.add(wallpaper);
            numWallpapers++;
        } while (cursor.moveToNext());
        c.setNumWallpapers(numWallpapers);
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

@Nullable
public Wallpaper getRandomWallpaper() {
    Wallpaper wallpaper = null;//from   ww w. j  a  v a 2 s .  co  m
    List<String> selected = getSelectedCategories();
    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.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getInt(6) == 1,
                    cursor.getLong(7));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpaper;
}

From source file:cl.gisred.android.RepartoActivity.java

public void readCountData() {

    SQLiteDatabase db = sqlReparto.getReadableDatabase();
    String[] sValues = { "id", "codigo", "x", "y" };
    Cursor cData = db.query("repartos", sValues, null, null, null, null, null, null);

    if (cData != null && cData.getCount() >= 0) {
        iContRep = cData.getCount();// www  .j  av  a 2s  .c o  m
        cData.close();
    }

    db.close();
}

From source file:com.spoiledmilk.ibikecph.util.DB.java

public int favoritesForName(String name) {
    int ret = 0;//  w  ww.  j a v a2s  .c  o  m
    SQLiteDatabase db = getReadableDatabase();
    if (db != null) {
        String[] columns = { KEY_NAME };
        Cursor cursor = db.query(TABLE_FAVORITES, columns, KEY_NAME + " = ? ", new String[] { "" + name }, null,
                null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            ret = cursor.getCount();
        }
        if (cursor != null)
            cursor.close();
        db.close();
    }
    return ret;
}

From source file:cl.gisred.android.RepartoActivity.java

public void readData() {

    SQLiteDatabase db = sqlReparto.getReadableDatabase();
    String[] sValues = { "id", "codigo", "x", "y" };
    Cursor cData = db.query("repartos", sValues, null, null, null, null, null, null);
    arrayDatos = new ArrayList<>();

    if (cData != null && cData.getCount() > 0) {
        iContRep = cData.getCount();//from w  w  w .ja v  a  2 s .c  om

        cData.moveToFirst();

        do {
            RepartoClass oRep = new RepartoClass(cData.getInt(0), cData.getString(1), cData.getDouble(2),
                    cData.getDouble(3));

            arrayDatos.add(oRep);
        } while (cData.moveToNext());

        cData.close();
    }

    db.close();
}

From source file:com.spoiledmilk.ibikecph.util.DB.java

public long saveSearchHistory(HistoryData hd, HistoryData from, Context context) {
    SQLiteDatabase db = this.getWritableDatabase();
    if (db == null)
        return -1;

    String[] columns = { KEY_ID, KEY_NAME };
    long id;/*www  .  j a va  2  s  .  com*/
    Cursor cursor = db.query(TABLE_SEARCH_HISTORY, columns, KEY_NAME + " = ?", new String[] { hd.getName() },
            null, null, null, null);
    if (cursor == null || cursor.isAfterLast()) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, hd.getName());
        values.put(KEY_ADDRESS, hd.getAdress());
        values.put(KEY_START_DATE, hd.getStartDate());
        values.put(KEY_END_DATE, hd.getEndDate());
        values.put(KEY_SOURCE, hd.getSource());
        values.put(KEY_SUBSOURCE, hd.getSubSource());
        values.put(KEY_LAT, Double.valueOf(hd.getLatitude()));
        values.put(KEY_LONG, Double.valueOf(hd.getLongitude()));
        id = db.insert(TABLE_SEARCH_HISTORY, null, values);
    } else {
        cursor.moveToFirst();
        id = cursor.getInt(cursor.getColumnIndex(KEY_ID));

    }
    if (cursor != null)
        cursor.close();

    db.close();

    if (context != null && from != null)
        postHistoryItemToServer(hd, from, context);

    return id;
}

From source file:com.mk4droid.IMC_Services.DatabaseHandler.java

/**
 * Get issue picture from SQlite according to issue id
 * //ww w .  j  a  v a 2  s.co  m
 * @param IssueID
 * @return
 */
public IssuePic getIssuePic(int IssueID) {
    SQLiteDatabase db = this.getReadableDatabase();
    IssuePic mIssuePic;

    if (!db.isOpen())
        db = this.getWritableDatabase();

    Cursor cr = db.query(TABLE_IssuesPics, new String[] { KEY_IssueID, KEY_IssuePicData }, KEY_IssueID + "=?",
            new String[] { Integer.toString(IssueID) }, null, null, null, null);

    boolean ExistsRes = cr.moveToFirst();

    if (!ExistsRes) {
        mIssuePic = new IssuePic(-1, null);
    } else {

        mIssuePic = new IssuePic(cr.getInt(0), cr.getBlob(1));
    }
    cr.close();
    if (db.isOpen())
        db.close();

    return mIssuePic;
}

From source file:com.mk4droid.IMC_Services.DatabaseHandler.java

/**
 *  Get Issue Thumb from SQLite table according to issue id.
 * //from  w w  w.  ja v  a  2 s .co  m
 * @param IssueID
 * @return
 */
public IssuePic getIssueThumb(int IssueID) {
    SQLiteDatabase db = this.getReadableDatabase();
    IssuePic mIssueThumb;

    if (!db.isOpen())
        db = this.getWritableDatabase();

    Cursor cr = db.query(TABLE_IssuesThumbs, new String[] { KEY_IssueID, KEY_IssueThumbData },
            KEY_IssueID + "=?", new String[] { Integer.toString(IssueID) }, null, null, null, null);

    boolean ExistsRes = cr.moveToFirst();

    if (!ExistsRes) {
        mIssueThumb = new IssuePic(-1, null);
    } else {
        mIssueThumb = new IssuePic(cr.getInt(0), cr.getBlob(1));
    }
    cr.close();

    if (db.isOpen())
        db.close();

    return mIssueThumb;
}