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:me.piebridge.bible.Bible.java

/**
 * load notes, called when osis is changed
 * @param osis book.chapter//from  w w w  .  jav  a 2  s  .  c o  m
 */
public void loadNotes(String osis) {
    if (osis == null) {
        return;
    }
    if (this.osis != null && this.osis.equals(osis)) {
        return;
    }
    this.osis = osis;
    notes.clear();
    if (mOpenHelper == null) {
        mOpenHelper = new AnnotationsDatabaseHelper(mContext);
    }
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    if (!isDatabaseIntegrityOk(db)) {
        return;
    }

    Cursor cursor = null;
    try {
        cursor = db.query(AnnotationsDatabaseHelper.TABLE_ANNOTATIONS, null, "osis = ? and type = ?",
                new String[] { osis, "note" }, null, null, null);
        while (cursor != null && cursor.moveToNext()) {
            long id = cursor.getInt(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_ID));
            String verse = cursor.getString(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_VERSE));
            String verses = cursor.getString(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_VERSES));
            String content = cursor.getString(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_CONTENT));
            Long create = cursor.getLong(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_CONTENT));
            Long update = cursor.getLong(cursor.getColumnIndex(AnnotationsDatabaseHelper.COLUMN_UPDATETIME));
            notes.put(verse, new Note(id, verse, verses, content, create, update));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:ru.orangesoftware.financisto2.db.DatabaseAdapter.java

public void recalculateAccountsBalances() {
    SQLiteDatabase db = db();
    db.beginTransaction();/*from ww w .j  av  a 2s .co m*/
    try {
        Cursor accountsCursor = db.query(ACCOUNT_TABLE, new String[] { AccountColumns.ID }, null, null, null,
                null, null);
        try {
            while (accountsCursor.moveToNext()) {
                long accountId = accountsCursor.getLong(0);
                recalculateAccountBalances(accountId);
            }
        } finally {
            accountsCursor.close();
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

From source file:com.odoo.orm.OModel.java

/**
 * Gets the creates the id.// w w  w.  j  a  v  a  2 s .c  o  m
 * 
 * @return the creates the id
 */
private int getCreateId() {
    int newId = 0;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cr = db.query("sqlite_sequence", new String[] { "name", "seq" }, "name = ?",
            new String[] { getTableName() }, null, null, null);
    if (cr.moveToFirst()) {
        newId = cr.getInt(cr.getColumnIndex("seq"));
    }
    cr.close();
    db.close();
    return newId;
}

From source file:com.odoo.orm.OModel.java

/**
 * Select one to many rel ids./*from  w  w w  .j a v a2  s . c  o  m*/
 * 
 * @param base
 *            the base
 * @param rel
 *            the rel
 * @param base_id
 *            the base_id
 * @param ref_column
 *            the ref_column
 * @return the list
 */
public List<Integer> selecto2MRelIds(OModel base, OModel rel, int base_id, String ref_column) {
    List<Integer> ids = new ArrayList<Integer>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cr = db.query(rel.getTableName(), new String[] { "id" }, ref_column + " = ?",
            new String[] { base_id + "" }, null, null, null);
    if (cr.moveToFirst()) {
        do {
            ids.add(cr.getInt(cr.getColumnIndex("id")));
        } while (cr.moveToNext());
    }
    cr.close();
    db.close();
    return ids;
}

From source file:com.odoo.orm.OModel.java

/**
 * Gets the name.//from  w ww  . j  ava 2 s . c o m
 * 
 * @param model
 *            the model
 * @param row_id
 *            the row_id
 * @return the name
 */
public String getName(int row_id) {
    String name = "false";
    if (getColumn("name") != null) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cr = db.query(getTableName(), new String[] { "name" }, OColumn.ROW_ID + " = ?",
                new String[] { row_id + "" }, null, null, null);
        if (cr.moveToFirst()) {
            name = cr.getString(cr.getColumnIndex("name"));
        }
        cr.close();
        db.close();
    }
    return name;
}

From source file:com.openerp.orm.ORM.java

/**
 * Execute query./*from   ww  w  .jav a  2  s  . c  om*/
 * 
 * @param dbHelper
 *            the db helper
 * @param where
 *            the where
 * @param whereVals
 *            the where vals
 * @param group_by
 *            the group_by
 * @param having
 *            the having
 * @param orderby
 *            the orderby
 * @return the list
 */
private List<HashMap<String, Object>> executeQuery(BaseDBHelper dbHelper, String[] fetch_columns,
        String[] where, String[] whereVals, String group_by, String having, String orderby) {
    SQLiteDatabase db = getWritableDatabase();
    List<String> cols = new ArrayList<String>();

    String columns[] = null;
    if (fetch_columns != null) {
        cols = new ArrayList<String>();
        cols = Arrays.asList(fetch_columns);
    } else {
        for (Fields col : dbHelper.getColumns()) {
            if (!(col.getType() instanceof Many2Many)) {
                cols.add(col.getName());
            }
        }
    }
    columns = cols.toArray(new String[cols.size()]);
    Cursor cursor = db.query(modelToTable(dbHelper.getModelName()), columns, whereStatement(where, dbHelper),
            whereVals, group_by, having, orderby);
    List<HashMap<String, Object>> data = getResult(dbHelper, columns, cursor);
    db.close();
    return data;
}

From source file:com.openatk.planting.MainActivity.java

public void loadOperations() {
    if (spinnerMenuAdapter != null)
        spinnerMenuAdapter.clear();//from   w w  w. j  av a 2  s .  c  o m
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    Cursor cursor = database.query(TableOperations.TABLE_NAME, TableOperations.COLUMNS, null, null, null, null,
            null);
    operationsList = new ArrayList<Operation>();
    while (cursor.moveToNext()) {
        Operation operation = Operation.cursorToOperation(cursor);
        if (operation != null)
            operationsList.add(operation);
        if (spinnerMenuAdapter != null) {
            if (operation != null)
                spinnerMenuAdapter.add(operation);
        }
    }
    cursor.close();
    dbHelper.close();
    if (operationsList.isEmpty() == false) {
        Operation operation = new Operation();
        operation.setId(null);
        operation.setName("New Operation");
        operationsList.add(operation);
        if (spinnerMenuAdapter != null)
            spinnerMenuAdapter.add(operation);
    } else {
        // Don't display any operation
        // TODO hide spinner menu
    }
    if (spinnerMenuAdapter != null)
        spinnerMenuAdapter.notifyDataSetChanged();
    selectCurrentOperationInSpinner();
}

From source file:com.odoo.orm.OModel.java

/**
 * Count.//from   w  w w.j  ava 2s  .c  o m
 * 
 * @param where
 *            the where
 * @param whereArgs
 *            the where args
 * @return the int
 */
public int count(String where, Object[] whereArgs) {

    int count = 0;
    SQLiteDatabase db = getReadableDatabase();
    String whr = getWhereClause(where);
    String[] args = getWhereArgs(whr, whereArgs);
    Cursor cr = db.query(getTableName(), new String[] { "count(*) as total" }, whr, args, null, null, null);
    cr.moveToFirst();
    count = cr.getInt(0);
    cr.close();
    db.close();
    return count;
}

From source file:com.odoo.orm.OModel.java

/**
 * Select many to many rel ids.//from  w  w w .j  a v  a 2 s .co m
 * 
 * @param base
 *            the base
 * @param rel
 *            the rel
 * @param base_id
 *            the base_id
 * @return the list
 */
public List<Integer> selectM2MRelIds(OModel base, OModel rel, int base_id) {
    List<Integer> ids = new ArrayList<Integer>();
    String table = base.getTableName() + "_" + rel.getTableName() + "_rel";
    String base_col = base.getTableName() + "_id";
    String rel_col = rel.getTableName() + "_id";
    SQLiteDatabase db = getReadableDatabase();
    String where = base_col + " = ? and odoo_name = ?";
    Object[] whereArgs = new Object[] { base_id, mUser.getAndroidName() };
    Cursor cr = db.query(table, new String[] { "*" }, getWhereClause(where), getWhereArgs(where, whereArgs),
            null, null, null);
    if (cr.moveToFirst()) {
        do {
            int rel_id = cr.getInt(cr.getColumnIndex(rel_col));
            if (rel.count(OColumn.ROW_ID + " = ?", new Object[] { rel_id }) > 0)
                ids.add(rel_id);
        } while (cr.moveToNext());
    }
    cr.close();
    db.close();
    return ids;
}

From source file:com.odoo.orm.OModel.java

/**
 * Select m2 m records.//from   w  w  w.ja v a2  s.c  om
 * 
 * @param base
 *            the base
 * @param rel
 *            the rel
 * @param base_id
 *            the base_id
 * @return the list
 */
public List<ODataRow> selectM2MRecords(OModel base, OModel rel, int base_id) {
    List<ODataRow> records = new ArrayList<ODataRow>();
    String table = base.getTableName() + "_" + rel.getTableName() + "_rel";
    String base_col = base.getTableName() + "_id";
    String rel_col = rel.getTableName() + "_id";
    SQLiteDatabase db = getReadableDatabase();
    String where = base_col + " = ? and odoo_name = ?";
    Object[] whereArgs = new Object[] { base_id, mUser.getAndroidName() };
    Cursor cr = db.query(table, new String[] { "*" }, getWhereClause(where), getWhereArgs(where, whereArgs),
            null, null, null);
    List<Integer> ids = new ArrayList<Integer>();
    if (cr.moveToFirst()) {
        do {
            int rel_id = cr.getInt(cr.getColumnIndex(rel_col));
            ids.add(rel_id);
        } while (cr.moveToNext());
    }
    cr.close();
    db.close();
    records.addAll(rel.select(OColumn.ROW_ID + " IN (" + StringUtils.repeat(" ?, ", ids.size() - 1) + "?)",
            new Object[] { ids }));
    return records;
}