Java Code Examples for android.database.sqlite.SQLiteCursor

The following examples show how to use android.database.sqlite.SQLiteCursor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: firebase-android-sdk   Source File: SQLitePersistence.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Uses the given binding arguments as positional parameters for the query.
 *
 * <p>Note that unlike {@link SQLiteDatabase#rawQuery}, this method takes Object binding
 * objects. Values in the <tt>args</tt> array need to be of a type that's usable in any of the
 * SQLiteProgram bindFoo methods.
 *
 * @return this Query object, for chaining.
 */
Query binding(Object... args) {
  // This is gross, but the best way to preserve both the readability of the caller (since
  // values don't have be arbitrarily converted to Strings) and allows BLOBs to be used as
  // bind arguments.
  //
  // The trick here is that since db.query and db.rawQuery take String[] bind arguments, we
  // need some other way to bind. db.execSQL takes Object[] bind arguments but doesn't actually
  // allow querying because it doesn't return a Cursor. SQLiteQuery does allow typed bind
  // arguments, but isn't directly usable.
  //
  // However, you can get to the SQLiteQuery indirectly by supplying a CursorFactory to
  // db.rawQueryWithFactory. The factory's newCursor method will be called with a new
  // SQLiteQuery, and now we can bind with typed values.

  cursorFactory =
      (db1, masterQuery, editTable, query) -> {
        bind(query, args);
        return new SQLiteCursor(masterQuery, editTable, query);
      };
  return this;
}
 
Example 2
Source Project: AvI   Source File: SQLiteAndroidDatabase.java    License: MIT License 6 votes vote down vote up
private void bindPreHoneycomb(JSONObject row, String key, Cursor cursor, int i) throws JSONException {
    // Since cursor.getType() is not available pre-honeycomb, this is
    // a workaround so we don't have to bind everything as a string
    // Details here: http://stackoverflow.com/q/11658239
    SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
    CursorWindow cursorWindow = sqLiteCursor.getWindow();
    int pos = cursor.getPosition();
    if (cursorWindow.isNull(pos, i)) {
        row.put(key, JSONObject.NULL);
    } else if (cursorWindow.isLong(pos, i)) {
        row.put(key, cursor.getLong(i));
    } else if (cursorWindow.isFloat(pos, i)) {
        row.put(key, cursor.getDouble(i));
    /* ** Read BLOB as Base-64 DISABLED in this branch:
    } else if (cursorWindow.isBlob(pos, i)) {
        row.put(key, new String(Base64.encode(cursor.getBlob(i), Base64.DEFAULT)));
    // ** Read BLOB as Base-64 DISABLED to HERE. */
    } else { // string
        row.put(key, cursor.getString(i));
    }
}
 
Example 3
Source Project: geopackage-android   Source File: GeoPackageCursorFactory.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver driver,
                        String editTable, SQLiteQuery query) {

    if (debugLogQueries) {
        Log.d(GeoPackageCursorFactory.class.getSimpleName(), query.toString());
    }

    // Create a standard cursor
    Cursor cursor = new SQLiteCursor(driver, editTable, query);

    // Wrap the cursor
    Cursor wrappedCursor = wrapCursor(cursor, editTable);

    return wrappedCursor;
}
 
Example 4
Source Project: geopackage-android   Source File: GeoPackageCursorFactory.java    License: MIT License 6 votes vote down vote up
/**
 * Get the SQLite Android Bindings cursor factory
 *
 * @return bindings cursor factory
 * @since 3.4.0
 */
public org.sqlite.database.sqlite.SQLiteDatabase.CursorFactory getBindingsCursorFactory() {
    return new org.sqlite.database.sqlite.SQLiteDatabase.CursorFactory() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Cursor newCursor(org.sqlite.database.sqlite.SQLiteDatabase db, org.sqlite.database.sqlite.SQLiteCursorDriver driver, String editTable, org.sqlite.database.sqlite.SQLiteQuery query) {

            if (debugLogQueries) {
                Log.d(GeoPackageCursorFactory.class.getSimpleName(), query.toString());
            }

            // Create a standard cursor
            Cursor cursor = new org.sqlite.database.sqlite.SQLiteCursor(driver, editTable, query);

            // Wrap the cursor
            Cursor wrappedCursor = wrapCursor(cursor, editTable);

            return wrappedCursor;
        }

    };
}
 
Example 5
Source Project: android_dbinspector   Source File: DatabaseHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compat method so we can get type of column on API < 11.
 * Source: http://stackoverflow.com/a/20685546/2643666
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getColumnType(Cursor cursor, int col) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
        CursorWindow cursorWindow = sqLiteCursor.getWindow();
        int pos = cursor.getPosition();
        int type = -1;
        if (cursorWindow.isNull(pos, col)) {
            type = FIELD_TYPE_NULL;
        } else if (cursorWindow.isLong(pos, col)) {
            type = FIELD_TYPE_INTEGER;
        } else if (cursorWindow.isFloat(pos, col)) {
            type = FIELD_TYPE_FLOAT;
        } else if (cursorWindow.isString(pos, col)) {
            type = FIELD_TYPE_STRING;
        } else if (cursorWindow.isBlob(pos, col)) {
            type = FIELD_TYPE_BLOB;
        }
        return type;
    } else {
        return cursor.getType(col);
    }
}
 
Example 6
Source Project: whassup   Source File: Whassup.java    License: Apache License 2.0 6 votes vote down vote up
public SQLiteDatabase openDatabase(final File dbFile) {
    return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), new SQLiteDatabase.CursorFactory() {
        @Override
        @SuppressWarnings("deprecation")
        public Cursor newCursor(final SQLiteDatabase db, SQLiteCursorDriver driver, String editTable, SQLiteQuery query) {
            return new SQLiteCursor(db, driver, editTable, query) {
                @Override
                public void close() {
                    Log.d(TAG, "closing cursor");
                    super.close();
                    db.close();
                    if (!dbFile.delete()) {
                        Log.w(TAG, "could not delete database " + dbFile);
                    }
                }
            };
        }
    }, SQLiteDatabase.OPEN_READWRITE);
}
 
Example 7
Source Project: alchemy   Source File: AndroidQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SQLiteIterator select() {
    mDb.acquireReference();
    try {
        final SQLiteCursorDriver driver = SQLiteCompat.newDriver(mDb, mSql);
        return new CursorIterator(new SQLiteCursor(driver, null, mQuery));
    } finally {
        mDb.releaseReference();
    }
}
 
Example 8
public static void upgradeCursorWindowSize(final Cursor cursor) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
        if (cursor instanceof AbstractWindowedCursor) {
            final AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor;
            windowedCursor.setWindow(new CursorWindow("4M", 4 * 1024 * 1024));
        }
        if (cursor instanceof SQLiteCursor) {
            ((SQLiteCursor) cursor).setFillWindowForwardOnly(true);
        }
    }
}
 
Example 9
Source Project: QuantumFlux   Source File: QuantumFluxCursorFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Cursor newCursor(SQLiteDatabase sqLiteDatabase, SQLiteCursorDriver sqLiteCursorDriver, String tableName, SQLiteQuery sqLiteQuery) {
    if (isDebugEnabled) {
        QuantumFluxLog.d(sqLiteQuery.toString());
    }

    return new SQLiteCursor(sqLiteCursorDriver, tableName, sqLiteQuery);
}
 
Example 10
Source Project: CPOrm   Source File: CPOrmCursorFactory.java    License: MIT License 5 votes vote down vote up
@Override
public Cursor newCursor(SQLiteDatabase sqLiteDatabase, SQLiteCursorDriver sqLiteCursorDriver, String tableName, SQLiteQuery sqLiteQuery) {

    if (debugEnabled) {
        CPOrmLog.d(sqLiteQuery.toString());
    }

    return new SQLiteCursor(sqLiteCursorDriver, tableName, sqLiteQuery);
}
 
Example 11
Source Project: ApkTrack   Source File: SugarCursorFactory.java    License: GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public Cursor newCursor(SQLiteDatabase sqLiteDatabase,
        SQLiteCursorDriver sqLiteCursorDriver,
        String editTable,
        SQLiteQuery sqLiteQuery) {

    if (debugEnabled) {
        Log.d("SQL Log", sqLiteQuery.toString());
    }

    return new SQLiteCursor(sqLiteDatabase, sqLiteCursorDriver, editTable, sqLiteQuery);
}
 
Example 12
Source Project: BobEngine   Source File: DBType.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public static int getType(Cursor cursor, int i) {
    SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
    CursorWindow cursorWindow = sqLiteCursor.getWindow();
    int pos = cursor.getPosition();
    int type = -1;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Honeycomb or later.
        type = cursor.getType(i);

        if (type == Cursor.FIELD_TYPE_BLOB) {
            type = FIELD_TYPE_BLOB;
        } else if (type == Cursor.FIELD_TYPE_FLOAT) {
            type = FIELD_TYPE_FLOAT;
        } else if (type == Cursor.FIELD_TYPE_INTEGER) {
            type = FIELD_TYPE_INTEGER;
        } else if (type == Cursor.FIELD_TYPE_NULL) {
            type = FIELD_TYPE_NULL;
        } else if (type == Cursor.FIELD_TYPE_STRING) {
            type = FIELD_TYPE_STRING;
        }
    } else {                                           // Before Honeycomb
        if (cursorWindow.isNull(pos, i)) {
            type = FIELD_TYPE_NULL;
        } else if (cursorWindow.isLong(pos, i)) {
            type = FIELD_TYPE_INTEGER;
        } else if (cursorWindow.isFloat(pos, i)) {
            type = FIELD_TYPE_FLOAT;
        } else if (cursorWindow.isString(pos, i)) {
            type = FIELD_TYPE_STRING;
        } else if (cursorWindow.isBlob(pos, i)) {
            type = FIELD_TYPE_BLOB;
        }
    }

    return type;
}
 
Example 13
Source Project: NexusData   Source File: DatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery,
                        String editTable, SQLiteQuery query) {
    LOG.debug(query.toString());

    // non-deprecated API is only available in API 11
    return new SQLiteCursor(db, masterQuery, editTable, query);
}
 
Example 14
Source Project: Conversations   Source File: CursorUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void upgradeCursorWindowSize(final Cursor cursor) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
        if (cursor instanceof AbstractWindowedCursor) {
            final AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor;
            windowedCursor.setWindow(new CursorWindow("4M", 4 * 1024 * 1024));
        }
        if (cursor instanceof SQLiteCursor) {
            ((SQLiteCursor) cursor).setFillWindowForwardOnly(true);
        }
    }
}
 
Example 15
Source Project: utexas-utilities   Source File: BuildingDatabase.java    License: Apache License 2.0 5 votes vote down vote up
public Cursor query(String tablename, String[] columns, String selection,
        String[] selectionArgs, String groupBy, String having, String orderBy) {
    if (selection != null) {
        SQLiteCursor c = (SQLiteCursor) getReadableDatabase().query(
                tablename,
                columns,
                KEY_SUGGEST_COLUMN_TEXT_1 + " LIKE '%" + selection + "%' OR "
                        + KEY_SUGGEST_COLUMN_TEXT_2 + " LIKE '%" + selection + "%'", selectionArgs,
                groupBy, having, orderBy);
        c.moveToFirst();
        return c;
    } else {
        return getReadableDatabase().query(tablename, columns, selection, selectionArgs, groupBy, having, orderBy);
    }
}
 
Example 16
Source Project: UTubeTV   Source File: CursorFactoryDebugger.java    License: The Unlicense 5 votes vote down vote up
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {
  if (debugQueries) {
    DUtils.log(query.toString());
  }
  return new SQLiteCursor(masterQuery, editTable, query);
}
 
Example 17
Source Project: squidb   Source File: SquidCursorFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {
    bindArgumentsToProgram(query, sqlArgs);
    return new SQLiteCursor(masterQuery, editTable, query);
}
 
Example 18
Source Project: AsymmetricGridView   Source File: DefaultCursorAdapter.java    License: MIT License 4 votes vote down vote up
@Override public Object getItem(int position) {
  return new CursorAdapterItem((SQLiteCursor) super.getItem(position));
}
 
Example 19
Source Project: AsymmetricGridView   Source File: DefaultCursorAdapter.java    License: MIT License 4 votes vote down vote up
CursorAdapterItem(SQLiteCursor cursor) {
  super(cursor.getInt(3), cursor.getInt(2), cursor.getInt(1));
}
 
Example 20
Source Project: android-schema-utils   Source File: SQLiteMaster.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable, SQLiteQuery query) {
  return new SQLiteCursor(db, masterQuery, editTable, query);
}