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:net.olejon.spotcommander.MyTools.java

public String[] getComputer(final long id) {
    final SQLiteDatabase mDatabase = new MainSQLiteHelper(mContext).getReadableDatabase();

    final String[] queryColumns = { MainSQLiteHelper.COLUMN_URI, MainSQLiteHelper.COLUMN_USERNAME,
            MainSQLiteHelper.COLUMN_PASSWORD };
    final Cursor mCursor = mDatabase.query(MainSQLiteHelper.TABLE_COMPUTERS, queryColumns,
            MainSQLiteHelper.COLUMN_ID + " = " + id, null, null, null, null);

    String uri = "";
    String username = "";
    String password = "";

    if (mCursor.moveToFirst()) {
        uri = mCursor.getString(mCursor.getColumnIndexOrThrow(MainSQLiteHelper.COLUMN_URI));
        username = mCursor.getString(mCursor.getColumnIndexOrThrow(MainSQLiteHelper.COLUMN_USERNAME));
        password = mCursor.getString(mCursor.getColumnIndexOrThrow(MainSQLiteHelper.COLUMN_PASSWORD));
    }//w  w w.j  a va2 s  .c  om

    mCursor.close();
    mDatabase.close();

    return new String[] { uri, username, password };
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public boolean hasHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;//from   w  w w. j  a va 2  s . c  om
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COUNT_COLUMN, null, null, null, null, null);
        cursor.moveToFirst();
        return cursor.getInt(0) > 0;
    } finally {
        close(cursor, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void deleteHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;//from   w  w w .java  2s.com
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number);
        db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
    } finally {
        close(cursor, db);
    }
}

From source file:fi.hut.soberit.sensors.services.BatchDataUploadService.java

protected void writeSession(final FileWriter destination, final Session session, boolean lastSession)
        throws IOException {

    final DatabaseHelper shardHelper = new DatabaseHelper(BatchDataUploadService.this, session.getId());
    final SQLiteDatabase db = shardHelper.getReadableDatabase();

    final Cursor rowCursor = db.query(DatabaseHelper.OBSERVATION_VALUE_TABLE, null, null, null, null, null,
            ObservationValueTable.TIME + " ASC");

    rowCursor.moveToFirst();//from www.j  av a  2  s  . c o m

    writeSessionHeader(destination, session, rowCursor);

    Log.d(TAG, "sessions: " + rowCursor.getCount());
    final StringBuilder builder = new StringBuilder();
    for (int row = 0; row < rowCursor.getCount(); row++) {
        rowCursor.moveToPosition(row);

        final GenericObservation observation = ObservationValueTable.observationFromCursor(rowCursor, row);

        final ObservationType type = typesMap.get(observation.getObservationTypeId());
        builder.setLength(0);

        writeObservation(builder, session, observation, type, row + 1 != rowCursor.getCount());

        destination.write(builder.toString());
    } // for(int row = 0; row < rowCursor.getCount(); row++) {

    writeSessionFooter(destination, session, rowCursor);

    rowCursor.close();
    shardHelper.close();

    if (!lastSession) {
        destination.write(",");
    }
}

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

/**
 * Load all of the unsynced requests, and set their status to in progress.
 * @return/* w w  w  . ja v  a  2 s  . c om*/
 */
private Cursor loadRequestTableContentsForUpdating() {
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        String[] unsychronizedSelectionArgs = { UNSYCHRONIZED.toString() };
        Cursor cursor = db.query(BOTH_DATABASE_TABLE_JOIN, RESULTS_COLUMNS, SYNCHRONIZED_VALUE_WHERE,
                unsychronizedSelectionArgs, null, null, requestColumn(KEY_REQUEST_ID));
        cursor.getCount(); //For some reason, accessing the cursor count before performing the update is required for the load to work. Doesn't make much sense unless it is ignoring order.
        ContentValues updatedValues = getUpdateSynchronizedContentValues(IN_PROGRESS);
        db.update(REQUEST_DATABASE_TABLE, updatedValues, SYNCHRONIZED_VALUE_WHERE, unsychronizedSelectionArgs);

        db.setTransactionSuccessful();

        return cursor;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        db.endTransaction();
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public HistoryItem buildHistoryItem(int number) {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;//from www .  j  a v a 2s  .co m
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(number + 1);
        String text = cursor.getString(0);
        String display = cursor.getString(1);
        String format = cursor.getString(2);
        long timestamp = cursor.getLong(3);
        // String details = cursor.getString(4);
        Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
        // return new HistoryItem(result, display, details);
        return new HistoryItem(result, display);
    } finally {
        close(cursor, db);
    }
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public List<HistoryItem> buildHistoryItems() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    List<HistoryItem> items = new ArrayList<HistoryItem>();
    SQLiteDatabase db = null;
    Cursor cursor = null;/*from  w ww .  j  ava 2s.  c  om*/
    try {
        db = helper.getReadableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, COLUMNS, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        while (cursor.moveToNext()) {
            String text = cursor.getString(0);
            String display = cursor.getString(1);
            String format = cursor.getString(2);
            long timestamp = cursor.getLong(3);
            // String details = cursor.getString(4);
            Result result = new Result(text, null, null, BarcodeFormat.valueOf(format), timestamp);
            // items.add(new HistoryItem(result, display, details));
            items.add(new HistoryItem(result, display));
        }
    } finally {
        close(cursor, db);
    }
    return items;
}

From source file:com.google.zxing.client.android.history.HistoryManager.java

public void trimHistory() {
    SQLiteOpenHelper helper = new DBHelper(activity);
    SQLiteDatabase db = null;
    Cursor cursor = null;/* ww  w.j  a  v  a 2s .  co m*/
    try {
        db = helper.getWritableDatabase();
        cursor = db.query(DBHelper.TABLE_NAME, ID_COL_PROJECTION, null, null, null, null,
                DBHelper.TIMESTAMP_COL + " DESC");
        cursor.move(MAX_ITEMS);
        while (cursor.moveToNext()) {
            String id = cursor.getString(0);
            Log.i(TAG, "Deleting scan history ID " + id);
            db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + id, null);
        }
    } catch (SQLiteException sqle) {
        // We're seeing an error here when called in
        // CaptureActivity.onCreate() in rare cases
        // and don't understand it. First theory is that it's transient so
        // can be safely ignored.
        Log.w(TAG, sqle);
        // continue
    } finally {
        close(cursor, db);
    }
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public ArrayList<ImageMessage> getImageMessages(String chatId) {
    ArrayList<ImageMessage> messages = new ArrayList<>();
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor c = db.query(chatId, new String[] { MessageHistoryContract.MessageEntry._ID },
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TYPE + "=?",
            new String[] { MessageHistory.TYPE_IMAGE }, null, null, null);
    c.moveToFirst();//from  w  ww .  j av  a2s .  c o  m
    do {
        long messageId = c.getLong(0);
        MessageArrayContent mac = getMessage(chatId, messageId);
        if (mac instanceof ImageMessage)
            messages.add((ImageMessage) mac);
    } while (c.move(1));
    c.close();
    db.close();
    return messages;
}

From source file:eu.rubenrosado.tinypasswordmanager.AllRows.java

public void selectAllRows() {
    DBHelper dbhelper;//from  www. j  a  va2s  .  c o  m
    SQLiteDatabase sql;
    dbhelper = new DBHelper(this);
    sql = dbhelper.getWritableDatabase();
    spArray = new ArrayList<ObjectRows>();
    decryptedPasswords = new ArrayList<String>();
    String[] columnes = { DBHelper.COLID, DBHelper.COLSITE, DBHelper.COLUSER, DBHelper.COLPASS };
    Cursor curs = sql.query(DBHelper.TABLENAME, columnes, null, null, null, null, null);
    try {
        while (curs.moveToNext()) {
            spArray.add(
                    new ObjectRows(curs.getInt(0), Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(1)),
                            Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(2)), HIDEPW));
            decryptedPasswords.add(Encryption.decrypt(Main.MASTERPASSWORD, curs.getString(3)));
        }
    } catch (Exception e) {
        toastMessage(getString(R.string.internal_error));
    } finally {
        sql.close();
        dbhelper.close();
    }
}