2016年1月29日 星期五

how and why SQLiteQueryBuilder

why SQLiteQueryBuilder?

SQLiteDatabase db = moviedbhelper.getReadableDatabase();
        Cursor cursor = db.query(MovieDbContract.FavouriteEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null);
        return cursor;


use the above method,we only can do some sample query.if we wan to do some difficult query,what can we do?

we need SQLiteQueryBuilder.

How to use?

1.create the SQLiteQueryBuilder

sWeatherByLocationSettingQueryBuilder = new SQLiteQueryBuilder();

2.if we want to inner join tow table,setTable

sWeatherByLocationSettingQueryBuilder.setTables(
                WeatherContract.WeatherEntry.TABLE_NAME + " INNER JOIN " +
                        WeatherContract.LocationEntry.TABLE_NAME +
                        " ON " + WeatherContract.WeatherEntry.TABLE_NAME +
                        "." + WeatherContract.WeatherEntry.COLUMN_LOC_KEY +
                        " = " + WeatherContract.LocationEntry.TABLE_NAME +
                        "." + WeatherContract.LocationEntry._ID);

3.use it
 Cursor cursor = sWeatherByLocationSettingQueryBuilder.query(mOpenHelper.getReadableDatabase(),
                projection,
                selection,
                selectionArgs,
                null,
                null,
                sortOrder
        );