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) 

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

public Map<String, Category> getCategoryMap() {
    Map<String, Category> categories = new HashMap<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {//w  w  w .jav a 2  s .  com
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1);
            categories.put(cursor.getString(1), category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

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

public List<Wallpaper> getWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, KEY_CATEGORY);
    if (cursor.moveToFirst()) {
        do {// w  w  w  . j a v a  2s.  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:com.maxwen.wallpaper.board.databases.Database.java

public List<Category> getCategories() {
    List<Category> categories = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {//from  ww  w  .  j  av  a2 s .  c  o m
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1);
            int count = getWallpapersCountOfCatgegory(cursor.getString(1));
            category.setNumWallpapers(count);
            categories.add(category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

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

public List<Object> getCategoriesUnified() {
    List<Object> categories = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {/* w  w  w . ja  v  a  2 s  . c  om*/
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1);
            int count = getWallpapersCountOfCatgegory(cursor.getString(1));
            category.setNumWallpapers(count);
            categories.add(category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

From source file:com.example.easyvoice.MessageDetail.java

public void LoadMessage(int msgId) {

    SQLiteDatabase db = null;
    try {//from  www . j  a  va2s  .  c o  m
        db = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);

        ContentValues values = new ContentValues();
        values.put("msg_id", msgId);

        String[] tableColumns = new String[] { "msg_id", "disp_name", "content" };
        String whereClause = "msg_id = ?";
        String[] whereArgs = new String[] { Integer.toString(msgId) };

        Cursor c = db.query(TABLE_NAME, tableColumns, whereClause, whereArgs, null, null, null);

        if (c != null) {
            Log.d(getClass().getSimpleName(), "cursor ok");

            boolean hasContents = c.moveToFirst();
            Log.i(getClass().getSimpleName(), "hasContents is " + hasContents);

            while (c.isAfterLast() == false) {

                Message m = new Message();

                int n = c.getColumnIndex("msg_id");
                Log.i(getClass().getSimpleName(), "reading column rowid, index is " + n);

                int rowid = c.getInt(n);
                m.setMsgId(rowid);

                Log.i(getClass().getSimpleName(), "reading column disp_name");
                m.setDispName(c.getString(c.getColumnIndex("disp_name")));
                Log.i(getClass().getSimpleName(), "reading column content");
                m.setContent(c.getString(c.getColumnIndex("content")));

                displayEdit.setText(m.getDispName());
                contentEdit.setText(m.getContent());
                /*      
                      map.put (pos, m);
                              
                      results.add(m.getDispName());
                */

                c.moveToNext();
            }
        }

        /*
        values.put("disp_name", displayEdit.getText ().toString());
        values.put("content",   contentEdit.getText ().toString());
        */
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database (2)");
    }
}

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

public List<Wallpaper> getFilteredWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    List<String> selected = getSelectedCategories();
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return wallpapers;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {/*  www . j a  v  a 2s  . c  o  m*/
            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, KEY_CATEGORY);
    if (cursor.moveToFirst()) {
        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);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

public List<Object> getFilteredCategoriesUnified() {
    List<Object> categories = new ArrayList<>();
    List<String> selected = getSelectedCategories();
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return categories;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_NAME).append(")").append(" LIKE ?");
        } else {/*ww w  . j  a va2 s .c o  m*/
            CONDITION.append("LOWER(").append(KEY_NAME).append(")").append(" LIKE ?");
        }
        selection.add("%" + item.toLowerCase(Locale.getDefault()) + "%");
    }
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, KEY_NAME);
    if (cursor.moveToFirst()) {
        do {
            Category category = new Category(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getInt(3) == 1);
            int count = getWallpapersCountOfCatgegory(cursor.getString(1));
            category.setNumWallpapers(count);
            categories.add(category);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return categories;
}

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

public List<Object> getWallpapersUnified() {
    List<Object> wallpapers = new ArrayList<>();
    Map<String, Category> categoryMap = getCategoryMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, KEY_CATEGORY);
    if (cursor.moveToFirst()) {
        String categoryName = cursor.getString(5);
        Category category = new Category(0, categoryName, categoryMap.get(categoryName).getThumbUrl(), false);
        wallpapers.add(category);//ww  w .  java  2  s. c om
        int numWallpapers = 0;
        do {
            String newCategoryName = cursor.getString(5);
            if (!newCategoryName.equals(categoryName)) {
                category.setNumWallpapers(numWallpapers);
                numWallpapers = 0;
                categoryName = newCategoryName;
                category = new Category(0, newCategoryName, categoryMap.get(newCategoryName).getThumbUrl(),
                        false);
                wallpapers.add(category);
            }
            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());
        category.setNumWallpapers(numWallpapers);
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

public List<Wallpaper> getFavoriteWallpapers() {
    List<Wallpaper> wallpapers = new ArrayList<>();
    List<String> selected = getSelectedCategories();
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return wallpapers;

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {//from   w w  w.ja v a  2 s . c om
            CONDITION.append("(LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        }
        selection.add("%" + item.toLowerCase(Locale.getDefault()) + "%");
    }
    CONDITION.append(") AND " + KEY_FAVORITE + " = ?");
    selection.add("1");
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, KEY_CATEGORY);
    if (cursor.moveToFirst()) {
        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);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

public List<Object> getFilteredWallpapersUnified() {
    List<Object> wallpapers = new ArrayList<>();
    List<String> selected = getSelectedCategories();
    List<String> selection = new ArrayList<>();
    if (selected.size() == 0)
        return wallpapers;
    Map<String, Category> categoryMap = getCategoryMap();

    StringBuilder CONDITION = new StringBuilder();
    for (String item : selected) {
        if (CONDITION.length() > 0) {
            CONDITION.append(" OR ").append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
        } else {//from  w w  w  . j  a v a  2 s  .c  o m
            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, KEY_CATEGORY);
    if (cursor.moveToFirst()) {
        String categoryName = cursor.getString(5);
        Category category = new Category(0, categoryName, categoryMap.get(categoryName).getThumbUrl(), false);
        wallpapers.add(category);
        int numWallpapers = 0;
        do {
            String newCategoryName = cursor.getString(5);
            if (!newCategoryName.equals(categoryName)) {
                category.setNumWallpapers(numWallpapers);
                numWallpapers = 0;
                categoryName = newCategoryName;
                category = new Category(0, newCategoryName, categoryMap.get(newCategoryName).getThumbUrl(),
                        false);
                wallpapers.add(category);
            }
            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());
        category.setNumWallpapers(numWallpapers);
    }
    cursor.close();
    db.close();
    return wallpapers;
}