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:org.disrupted.rumble.database.statistics.StatisticDatabase.java

public JSONArray getJSON() {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    Cursor cursor = database.query(getTableName(), null, null, null, null, null, null);
    if (cursor == null)
        return null;
    JSONArray resultSet = new JSONArray();
    try {//from   w w  w.j a  v  a 2s.  c o m
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();
            for (int i = 0; i < totalColumn; i++) {
                if (cursor.getColumnName(i) != null) {
                    try {
                        if (cursor.getString(i) != null)
                            rowObject.put(cursor.getColumnName(i), cursor.getString(i));
                        else
                            rowObject.put(cursor.getColumnName(i), "");
                    } catch (Exception e) {
                    }
                }
            }
            resultSet.put(rowObject);
            cursor.moveToNext();
        }
        cursor.close();
    } finally {
        cursor.close();
    }
    return resultSet;
}

From source file:at.bitfire.nophonespam.CallReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction()) && intent
            .getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i(TAG, "Received call: " + incomingNumber);

        Settings settings = new Settings(context);
        if (TextUtils.isEmpty(incomingNumber)) {
            // private number (no caller ID)
            if (settings.blockHiddenNumbers())
                rejectCall(context, null);

        } else {/*from w w w.java  2  s .c o  m*/
            DbHelper dbHelper = new DbHelper(context);
            try {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor c = db.query(Number._TABLE, null, "? LIKE " + Number.NUMBER,
                        new String[] { incomingNumber }, null, null, null);
                if (c.moveToNext()) {
                    ContentValues values = new ContentValues();
                    DatabaseUtils.cursorRowToContentValues(c, values);
                    Number number = Number.fromValues(values);

                    rejectCall(context, number);

                    values.clear();
                    values.put(Number.LAST_CALL, System.currentTimeMillis());
                    values.put(Number.TIMES_CALLED, number.timesCalled + 1);
                    db.update(Number._TABLE, values, Number.NUMBER + "=?", new String[] { number.number });

                    BlacklistObserver.notifyUpdated();
                }
                c.close();
            } finally {
                dbHelper.close();
            }
        }
    }
}

From source file:company.test.Test.java

public void updateUI() {
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(ItemContract.TABLE,
            new String[] { ItemContract.Columns._ID, ItemContract.Columns.ITEM }, null, null, null, null, null);
    ListAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.item_view, cursor,
            new String[] { ItemContract.Columns.ITEM }, new int[] { R.id.taskTextView }, 0);
    listView.setAdapter(listAdapter);//from  w  ww .j a v  a2  s  .  c o  m
}

From source file:com.github.gw2app.events.Gw2StaticData.java

private void _update_event_names() {
    if (Gw2JSONDownloader.internetAvailable(mContext)) {
        String result = Gw2JSONDownloader.downloadJSON(Gw2JSONDownloader.event_names_url);

        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        Cursor cursor = db.query(Gw2DB.EVENT_NAMES_TABLE, null, null, null, null, null, null);

        JSONArray jsData;/*from   ww  w  .j av a  2s . co  m*/

        try {
            jsData = new JSONArray(result);
            Log.d("Gw2", "LENGTH: " + jsData.length());
            Log.d("Gw2", "POCESSING STARTS");
            for (int i = 0; i < jsData.length(); i++) {
                JSONObject obj = jsData.getJSONObject(i);
                String id = obj.getString("id");
                String name = obj.getString("name");
                //Log.d("Gw2", name);

                ContentValues row = new ContentValues(3);
                row.put("_id", id);
                row.put("name", name);
                row.put("lang", "en");

                if (db != null) {
                    db.insert(Gw2DB.EVENT_NAMES_TABLE, null, row);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        //TODO: throw exception.
    }
}

From source file:org.jsharkey.oilcan.ScriptDatabase.java

/**
 * Return cursor across all scripts.//  ww  w .jav a2  s .c om
 */
public Cursor getScripts() {
    SQLiteDatabase db = this.getReadableDatabase();
    return db.query(TABLE_SCRIPTS, new String[] { "_id", FIELD_SCRIPT_NAME, FIELD_SCRIPT_AUTHOR,
            FIELD_SCRIPT_DESCRIP, FIELD_SCRIPT_DOMAINREGEX, FIELD_SCRIPT_ENABLED }, null, null, null, null,
            null);

}

From source file:com.quuzz.tbg.recyclerview.CustomAdapter.java

private TbModel[] initDataset() {
    // init DB/*from w w  w  .  ja  v a 2s  . c o  m*/
    // Gets the data repository in write mode
    //        String[] mDataset = new String[DATASET_COUNT];
    TbDbHelper tbDbHelper = new TbDbHelper(context);
    SQLiteDatabase db = tbDbHelper.getReadableDatabase();
    Cursor cursor = db.query("tbdata", null, null, null, null, null, null);
    int count = cursor.getCount();
    cursor.moveToFirst();
    TbModel[] mDataset = new TbModel[count];
    for (int i = 0; i < count; i++) {
        mDataset[i] = new TbModel();
        mDataset[i].setName(cursor.getString(cursor.getColumnIndex("name")));
        mDataset[i].setTbId(cursor.getString(cursor.getColumnIndex("tb_id")));
        mDataset[i].setImageData(cursor.getBlob(cursor.getColumnIndex("image_data")));
        mDataset[i].setDescription(cursor.getString(cursor.getColumnIndex("description")));
        cursor.moveToNext();
    }
    cursor.close();
    db.close();
    return mDataset;
}

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

public SparseArrayCompat<Request> getPremiumRequest() {
    SparseArrayCompat<Request> requests = new SparseArrayCompat<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_PREMIUM_REQUEST, null, null, null, null, null, null);
    if (cursor.moveToFirst()) {
        do {/*  ww w.  j a v a2s.  com*/
            Request request = new Request(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4));
            requests.append(requests.size(), request);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return requests;
}

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

public SparseArrayCompat<Wallpaper> getWallpapers() {
    SparseArrayCompat<Wallpaper> wallpapers = new SparseArrayCompat<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, null, null, null, null, KEY_ADDED_ON + " DESC, " + KEY_ID);
    if (cursor.moveToFirst()) {
        do {/*from  www  .  ja v a 2s  . c o  m*/
            Wallpaper wallpaper = new Wallpaper(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getString(4));
            wallpapers.append(wallpapers.size(), wallpaper);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return wallpapers;
}

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

public SparseArrayCompat<Wallpaper> getWallpaperAddedOn() {
    SparseArrayCompat<Wallpaper> dates = new SparseArrayCompat<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, new String[] { KEY_URL, KEY_ADDED_ON }, null, null, null, null,
            null);/*from w  ww.ja va 2  s.  c om*/
    if (cursor.moveToFirst()) {
        do {
            Wallpaper item = new Wallpaper(cursor.getString(0), cursor.getString(1));
            dates.append(dates.size(), item);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return dates;
}

From source file:com.example.android.touroflondon.data.TourDbHelper.java

/**
 * Returns a {@link Cursor} for all POIs with the given projection.
 *
 * @param projection/*from  ww  w.j ava  2s.  c o m*/
 * @return
 */
public Cursor getAllPoi(String[] projection) {
    SQLiteDatabase db = this.getReadableDatabase();

    // select all entries from POI table
    return db.query(TourContract.PoiEntry.TABLE_NAME, projection, null, null, null, null, null);
}