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:uk.ac.horizon.ubihelper.service.PeersOpenHelper.java

public static List<PeerInfo> getPeerInfos(SQLiteDatabase database, String where, String values[]) {
    Cursor c = database.query(PEER_TABLE_NAME, PEER_TABLE_COLUMNS, where, values, null, null,
            KEY_NAME + " ASC");
    LinkedList<PeerInfo> pis = new LinkedList<PeerInfo>();
    Log.d(TAG, "getPeerInfos returned " + c.getCount() + " rows");
    while (c.move(1)) {
        pis.add(getPeerInfo(c));//  ww  w.  j a v  a2 s .  co m
    }
    c.close();
    return pis;
}

From source file:uk.ac.horizon.ubihelper.service.PeersOpenHelper.java

public static PeerInfo getPeerInfo(SQLiteDatabase db, String id) {
    PeerInfo pi = null;/*www .j a  v a 2s  .c o  m*/
    try {
        Cursor c = db.query(PEER_TABLE_NAME, PEER_TABLE_COLUMNS, "ID = ?", new String[] { id }, null, null,
                null);
        try {
            if (c.getCount() > 0) {
                c.moveToFirst();
                pi = getPeerInfo(c);
            }
        } finally {
            c.close();
        }
    } catch (Exception e) {
        Log.w(TAG, "Error looking for PeerInfo id=" + id + ": " + e);
    }
    return pi;
}

From source file:com.onesignal.NotificationOpenedProcessor.java

private static void addChildNotifications(JSONArray dataArray, String summaryGroup, SQLiteDatabase writableDb) {
    String[] retColumn = { NotificationTable.COLUMN_NAME_FULL_DATA };
    String[] whereArgs = { summaryGroup };

    Cursor cursor = writableDb.query(NotificationTable.TABLE_NAME, retColumn,
            NotificationTable.COLUMN_NAME_GROUP_ID + " = ? AND " + // Where String
                    NotificationTable.COLUMN_NAME_DISMISSED + " = 0 AND " + NotificationTable.COLUMN_NAME_OPENED
                    + " = 0 AND " + NotificationTable.COLUMN_NAME_IS_SUMMARY + " = 0",
            whereArgs, null, null, null);

    if (cursor.getCount() > 1) {
        cursor.moveToFirst();//ww w .j  a  va  2 s.co  m
        do {
            try {
                String jsonStr = cursor
                        .getString(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_FULL_DATA));
                dataArray.put(new JSONObject(jsonStr));
            } catch (Throwable t) {
                OneSignal.Log(OneSignal.LOG_LEVEL.ERROR,
                        "Could not parse JSON of sub notification in group: " + summaryGroup);
            }
        } while (cursor.moveToNext());
    }

    cursor.close();
}

From source file:net.zionsoft.obadiah.model.translations.TranslationHelper.java

public static List<String> getBookNames(SQLiteDatabase db, String translationShortName) {
    Cursor cursor = null;//from ww w .j  a va2 s .  c  o  m
    try {
        cursor = db.query(DatabaseHelper.TABLE_BOOK_NAMES, new String[] { DatabaseHelper.COLUMN_BOOK_NAME },
                String.format("%s = ?", DatabaseHelper.COLUMN_TRANSLATION_SHORT_NAME),
                new String[] { translationShortName }, null, null,
                String.format("%s ASC", DatabaseHelper.COLUMN_BOOK_INDEX));
        final int bookName = cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_NAME);
        final List<String> bookNames = new ArrayList<String>(Bible.getBookCount());
        while (cursor.moveToNext())
            bookNames.add(cursor.getString(bookName));
        return bookNames;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:org.thinschema.dataaccess.JSONAdapter.java

/**
 * Get all data from table and convert them to JSON. Each row is converted
 * to its own JSONObject containing the names of the columns as keys, and
 * the contents as values. For example, if a table whose name is "People"
 * consists of two columns "FirstName" and "LastName", the generated JSON
 * would be:/* w ww  . j  a v a  2 s .  c om*/
 * </p>
 * <code>
 * { "name": "People", "rows": [ { "FirstName": "John", "LastName": "Doe" },
 * { "FirstName": "Susan", "LastName": "Appleseed" } ] }
 * </code>
 *
 * @param database  SQLiteDatabase instance.
 * @param tableName The name of the table.
 * @return JSONObject instance containing all records.
 */
public static JSONObject get(SQLiteDatabase database, String tableName) {

    JSONObject retval = new JSONObject();
    Cursor cursor = null;
    try {
        retval.put("name", tableName);
        cursor = database.query(tableName, null, null, null, null, null, null);

        // we get the list of all column names to make it easier when inserting key-value pairs
        String[] columnNames = cursor.getColumnNames();

        // iterate through each row
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            JSONArray data = new JSONArray();
            while (!cursor.isAfterLast()) {
                JSONObject row = new JSONObject();

                // for each column, store the key-value pair, using column name as key
                for (int i = 0, size = columnNames.length; i < size; ++i) {
                    // convert everything to a string
                    row.put(columnNames[i], cursor.getString(i));
                }

                data.put(row);
            }

            // add all of that to the result
            retval.put("rows", data);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return retval;
}

From source file:net.zionsoft.obadiah.model.translations.TranslationHelper.java

public static List<Verse> getVerses(SQLiteDatabase db, String translationShortName, String bookName, int book,
        int chapter) {
    Cursor cursor = null;/*from w w  w  .  j  a va 2 s.  c  o m*/
    try {
        cursor = db.query(translationShortName, new String[] { DatabaseHelper.COLUMN_TEXT },
                String.format("%s = ? AND %s = ?", DatabaseHelper.COLUMN_BOOK_INDEX,
                        DatabaseHelper.COLUMN_CHAPTER_INDEX),
                new String[] { Integer.toString(book), Integer.toString(chapter) }, null, null,
                String.format("%s ASC", DatabaseHelper.COLUMN_VERSE_INDEX));
        final int verse = cursor.getColumnIndex(DatabaseHelper.COLUMN_TEXT);
        final List<Verse> verses = new ArrayList<Verse>(cursor.getCount());
        int verseIndex = 0;
        while (cursor.moveToNext())
            verses.add(new Verse(book, chapter, verseIndex++, bookName, cursor.getString(verse)));
        return verses;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:net.zionsoft.obadiah.model.translations.TranslationHelper.java

@Nullable
public static Verse getVerse(SQLiteDatabase db, String translationShortName, String bookName, int book,
        int chapter, int verse) {
    Cursor cursor = null;//  w  ww .  ja v  a  2 s.co m
    try {
        cursor = db.query(translationShortName, new String[] { DatabaseHelper.COLUMN_TEXT },
                String.format("%s = ? AND %s = ? AND %s = ?", DatabaseHelper.COLUMN_BOOK_INDEX,
                        DatabaseHelper.COLUMN_CHAPTER_INDEX, DatabaseHelper.COLUMN_VERSE_INDEX),
                new String[] { Integer.toString(book), Integer.toString(chapter), Integer.toString(verse) },
                null, null, null);
        if (cursor.moveToFirst()) {
            return new Verse(book, chapter, verse, bookName, cursor.getString(0));
        } else {
            return null;
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:net.zionsoft.obadiah.model.translations.TranslationHelper.java

public static List<Verse> searchVerses(SQLiteDatabase db, String translationShortName, List<String> bookNames,
        String keyword) {/*from   ww w  .jav  a2 s . c om*/
    Cursor cursor = null;
    try {
        cursor = db.query(translationShortName,
                new String[] { DatabaseHelper.COLUMN_BOOK_INDEX, DatabaseHelper.COLUMN_CHAPTER_INDEX,
                        DatabaseHelper.COLUMN_VERSE_INDEX, DatabaseHelper.COLUMN_TEXT },
                String.format("%s LIKE ?", DatabaseHelper.COLUMN_TEXT),
                new String[] { String.format("%%%s%%", keyword.trim().replaceAll("\\s+", "%")) }, null, null,
                String.format(" %s ASC, %s ASC, %s ASC", DatabaseHelper.COLUMN_BOOK_INDEX,
                        DatabaseHelper.COLUMN_CHAPTER_INDEX, DatabaseHelper.COLUMN_VERSE_INDEX));
        final int count = cursor.getCount();
        if (count == 0)
            return Collections.emptyList();

        final int bookIndex = cursor.getColumnIndex(DatabaseHelper.COLUMN_BOOK_INDEX);
        final int chapterIndex = cursor.getColumnIndex(DatabaseHelper.COLUMN_CHAPTER_INDEX);
        final int verseIndex = cursor.getColumnIndex(DatabaseHelper.COLUMN_VERSE_INDEX);
        final int verseText = cursor.getColumnIndex(DatabaseHelper.COLUMN_TEXT);
        final List<Verse> verses = new ArrayList<Verse>(count);
        while (cursor.moveToNext()) {
            final int book = cursor.getInt(bookIndex);
            verses.add(new Verse(book, cursor.getInt(chapterIndex), cursor.getInt(verseIndex),
                    bookNames.get(book), cursor.getString(verseText)));
        }
        return verses;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

From source file:com.contentful.vault.SqliteHelper.java

static void deleteTables(SQLiteDatabase db) {
    String[] columns = new String[] { "name" };
    String selection = "type = ? AND name != ?";
    String[] args = new String[] { "table", "android_metadata" };
    Cursor cursor = db.query("sqlite_master", columns, selection, args, null, null, null);
    List<String> tables = null;
    try {/*from ww  w.j a  v a  2s  .  c o m*/
        if (cursor.moveToFirst()) {
            tables = new ArrayList<>();
            do {
                tables.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    if (tables != null) {
        db.beginTransaction();
        try {
            for (String table : tables) {
                db.execSQL("DROP TABLE " + escape(table));
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}

From source file:org.opendatakit.common.android.database.DataModelDatabaseHelper.java

/**
 * Accessor to retrieve the database table name given the tableId
 *
 * @param db//from   w w  w .  j av a  2 s.c om
 * @param tableId
 * @return
 */
public static String getDbTableName(SQLiteDatabase db, String tableId) {
    Cursor c = null;
    try {
        c = db.query(TABLE_DEFS_TABLE_NAME, new String[] { TableDefinitionsColumns.DB_TABLE_NAME },
                TableDefinitionsColumns.TABLE_ID + "=?", new String[] { tableId }, null, null, null);

        if (c.moveToFirst()) {
            int idx = c.getColumnIndex(TableDefinitionsColumns.DB_TABLE_NAME);
            return c.getString(idx);
        }
    } finally {
        if (c != null && !c.isClosed()) {
            c.close();
        }
    }
    return null;
}