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.jsharkey.oilcan.ScriptDatabase.java

/**
 * Toggle the enabled state of the given script.
 *//*from w ww  .ja va 2  s . c  o  m*/
public void toggleEnabled(long id) {
    SQLiteDatabase db = this.getWritableDatabase();

    // first read the existing status
    Cursor cur = db.query(TABLE_SCRIPTS, new String[] { FIELD_SCRIPT_ENABLED }, "_id = ?",
            new String[] { Long.toString(id) }, null, null, null);
    if (cur == null || !cur.moveToFirst())
        return;
    int value = cur.getInt(cur.getColumnIndex(FIELD_SCRIPT_ENABLED));
    cur.close();

    // update to have the opposite value
    ContentValues values = new ContentValues();
    values.put(FIELD_SCRIPT_ENABLED, (value == 0) ? 1 : 0);
    db.update(TABLE_SCRIPTS, values, "_id = ?", new String[] { Long.toString(id) });

}

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

private List<Gw2Event> _getEvents(String url) {
    Log.d("Gw2", "Fetching event names from DB.");
    SQLiteDatabase db = this.dbhelper.getWritableDatabase();
    Cursor cursor = db.query(Gw2DB.EVENT_NAMES_TABLE, null, null, null, null, null, null);
    HashMap<String, String> eventNames = new HashMap<String, String>();

    if (cursor.getCount() == 0) {
        cursor.close();// w  w  w. j a va2s.  c om
        return new ArrayList<Gw2Event>();
    }

    cursor.moveToFirst();
    while (!cursor.isLast()) {
        String id = cursor.getString(0);
        String name = cursor.getString(1);
        eventNames.put(id, name);
        cursor.moveToNext();
    }
    cursor.close();

    Log.d("Gw2", "Fetching event data from JSON");
    ArrayList<Gw2Event> list = new ArrayList<Gw2Event>();
    try {
        //Log.d("Gw2", url);
        String result = this.fetchJSONfromURL(url);
        JSONObject jsData = new JSONObject(result);
        JSONArray jsArray = jsData.getJSONArray("events");

        for (int i = 0; i < jsArray.length(); i++) {
            JSONObject obj = jsArray.getJSONObject(i);
            int world_id = obj.getInt("world_id");
            int map_id = obj.getInt("map_id");
            String event_id = obj.getString("event_id");
            String state = obj.getString("state");
            list.add(new Gw2Event(world_id, map_id, event_id, state, eventNames.get(event_id)));
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return list;
}

From source file:org.thomnichols.android.gmarks.WebViewCookiesDB.java

List<Cookie> getCookies() {
    List<Cookie> cookies = new ArrayList<Cookie>();
    SQLiteDatabase db = openDatabase(SQLiteDatabase.OPEN_READONLY);
    if (db == null)
        return cookies;

    try {/*w w w  . ja  va  2s.  co  m*/
        db.execSQL("PRAGMA read_uncommitted = true;");
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);

        while (cursor.moveToNext()) {
            BasicClientCookie c = new BasicClientCookie(cursor.getString(COL_NAME),
                    cursor.getString(COL_VALUE));
            c.setDomain(cursor.getString(COL_DOMAIN));
            c.setPath(cursor.getString(COL_PATH));
            Long expiry = cursor.getLong(COL_EXPIRES);
            if (expiry != null)
                c.setExpiryDate(new Date(expiry));
            c.setSecure(cursor.getShort(COL_SECURE) == 1);
            Log.d(TAG, "Got cookie: " + c.getName());
            cookies.add(c);
        }
        cursor.close();

        //         cursor = db.query(TABLE_NAME, new String[] {"count(name)"}, null, null, null, null, null);
        //         cursor.moveToFirst();
        //         Log.d("WEBVIEW DB QUERY", "COunt: " + cursor.getLong(0) );
        //          cursor.close();
        return cookies;
    } finally {
        db.close();
    }
}

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

private boolean _hasWorldNamesInDB() {
    SQLiteDatabase db = this.dbhelper.getReadableDatabase();
    Cursor cursor;//w w  w . j  a  v a2  s .co  m
    if (db != null) {
        cursor = db.query(Gw2DB.WORLD_NAMES_TABLE, null, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            cursor.close();
            return false;
        } else {
            cursor.close();
            return true;
        }
    }
    return false;
}

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

private boolean _hasMapNamesInDB() {
    SQLiteDatabase db = this.dbhelper.getReadableDatabase();
    Cursor cursor;//  w w  w . j ava 2 s. c  om
    if (db != null) {
        cursor = db.query(Gw2DB.MAP_NAMES_TABLE, null, null, null, null, null, null);
        if (cursor.getCount() == 0) {
            cursor.close();
            return false;
        } else {
            cursor.close();
            return true;
        }
    }
    return false;
}

From source file:net.olejon.mdapp.AtcCodesActivity.java

private void getSubstance(String substanceName) {
    SQLiteDatabase sqLiteDatabase = new SlDataSQLiteHelper(mContext).getReadableDatabase();

    String[] queryColumns = { SlDataSQLiteHelper.SUBSTANCES_COLUMN_ID };
    Cursor cursor = sqLiteDatabase.query(SlDataSQLiteHelper.TABLE_SUBSTANCES, queryColumns,
            SlDataSQLiteHelper.SUBSTANCES_COLUMN_NAME + " = " + mTools.sqe(substanceName), null, null, null,
            SlDataSQLiteHelper.SUBSTANCES_COLUMN_ID);

    if (cursor.moveToFirst()) {
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(SlDataSQLiteHelper.SUBSTANCES_COLUMN_ID));

        Intent intent = new Intent(mContext, SubstanceActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);//ww w .ja v  a  2 s . co m
    } else {
        cursor = sqLiteDatabase.query(SlDataSQLiteHelper.TABLE_SUBSTANCES, queryColumns,
                SlDataSQLiteHelper.SUBSTANCES_COLUMN_NAME + " LIKE " + mTools.sqe("%" + substanceName + "%"),
                null, null, null, SlDataSQLiteHelper.SUBSTANCES_COLUMN_ID);

        if (cursor.moveToFirst()) {
            long id = cursor.getLong(cursor.getColumnIndexOrThrow(SlDataSQLiteHelper.SUBSTANCES_COLUMN_ID));

            Intent intent = new Intent(mContext, SubstanceActivity.class);
            intent.putExtra("id", id);
            startActivity(intent);
        } else {
            mTools.showToast(getString(R.string.atc_codes_could_not_find_substance), 1);
        }
    }

    cursor.close();
    sqLiteDatabase.close();
}

From source file:net.survivalpad.android.entity.AbsData.java

public void findAll(SQLiteDatabase db, List<T> out) {
    String table = getTableName();
    String[] columns = getAllColumns();
    String selection = null;// w w w . j  ava2  s.  c  o m
    String[] selectionArgs = null;
    String groupBy = null;
    String having = null;
    String orderBy = "_id";

    Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    while (cursor.moveToNext()) {
        T obj = getInstance();
        obj.read(cursor);
        out.add(obj);
    }
}

From source file:com.cloudmine.api.db.RequestDBOpenHelper.java

public LinkedHashMap<Integer, RequestDBObject> retrieveAllRequests() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(BOTH_DATABASE_TABLE_JOIN, RESULTS_COLUMNS, null, null, null, null,
            requestColumn(KEY_REQUEST_ID));
    return createRequestMapping(cursor);
}

From source file:com.citrus.sdk.database.DBHandler.java

public List<BankOptions> getBankOptions() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor current_cursor;//from w  w  w. ja  v  a 2 s .c o m
    String[] columNames = { BANK, BANK_CID };
    List<BankOptions> bankOptionsList = new ArrayList<BankOptions>();
    current_cursor = db.query(BANK_TABLE, columNames, null, null, null, null, null);
    if (current_cursor.moveToFirst()) {
        do {
            BankOptions currentOption = new BankOptions(current_cursor.getString(0),
                    current_cursor.getString(1));
            bankOptionsList.add(currentOption);
        } while (current_cursor.moveToNext());
    }
    return bankOptionsList;
}

From source file:net.survivalpad.android.entity.AbsData.java

public void findById(long id, SQLiteDatabase db) {
    String table = getTableName();
    String[] columns = getAllColumns();
    String selection = "_id = ?";
    String[] selectionArgs = new String[] { String.valueOf(id) };
    String groupBy = null;//from   w w w . j  av  a 2  s. co  m
    String having = null;
    String orderBy = "_id";

    Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    if (cursor.moveToNext()) {
        read(cursor);
    }
}