/** * This database is only a cache for online data, so its upgrade policy is simply to discard * the data and call through to onCreate to recreate the table. Note that this only fires if * you change the version number for your database (in our case, DATABASE_VERSION). It does NOT * depend on the version number for your application found in your app/build.gradle file. If * you want to update the schema without wiping data, commenting out the current body of this * method should be your top priority before modifying this method. * * @param sqLiteDatabase Database that is being upgraded * @param oldVersion The old database version * @param newVersion The new database version */ @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); onCreate(sqLiteDatabase); } }
/** * You do not need to call this method. This is a method specifically to assist the testing * framework in running smoothly. You can read more at: * http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() */ @Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); } }
/** * This method will clear all rows from the weather table in our database. * <p> * Please note: * <p> * - This does NOT delete the table itself. We call this method from our @Before annotated * method to clear all records from the database before each test on the ContentProvider. * <p> * - We don't use the ContentProvider's delete functionality to perform this row deletion * because in this class, we are attempting to test the ContentProvider. We can't assume * that our ContentProvider's delete method works in our ContentProvider's test class. */ private void deleteAllRecordsFromWeatherTable() { /* Access writable database through WeatherDbHelper */ WeatherDbHelper helper = new WeatherDbHelper(InstrumentationRegistry.getTargetContext()); SQLiteDatabase database = helper.getWritableDatabase(); /* The delete method deletes all of the desired rows from the table, not the table itself */ database.delete(WeatherContract.WeatherEntry.TABLE_NAME, null, null); /* Always close the database when you're through with it */ database.close(); } }
/** * In onCreate, we initialize our content provider on startup. This method is called for all * registered content providers on the application main thread at application launch time. * It must not perform lengthy operations, or application startup will be delayed. * * Nontrivial initialization (such as opening, upgrading, and scanning * databases) should be deferred until the content provider is used (via {@link #query}, * {@link #bulkInsert(Uri, ContentValues[])}, etc). * * Deferred initialization keeps application startup fast, avoids unnecessary work if the * provider turns out not to be needed, and stops database errors (such as a full disk) from * halting application launch. * * @return true if the provider was successfully loaded, false otherwise */ @Override public boolean onCreate() { /* * As noted in the comment above, onCreate is run on the main thread, so performing any * lengthy operations will cause lag in your app. Since WeatherDbHelper's constructor is * very lightweight, we are safe to perform that initialization here. */ mOpenHelper = new WeatherDbHelper(getContext()); return true; }
numRowsDeleted = mOpenHelper.getWritableDatabase().delete( WeatherContract.WeatherEntry.TABLE_NAME, selection,
cursor = mOpenHelper.getReadableDatabase().query( cursor = mOpenHelper.getReadableDatabase().query( WeatherContract.WeatherEntry.TABLE_NAME, projection,
/** * This method will clear all rows from the weather table in our database. * <p> * Please note: * <p> * - This does NOT delete the table itself. We call this method from our @Before annotated * method to clear all records from the database before each test on the ContentProvider. * <p> * - We don't use the ContentProvider's delete functionality to perform this row deletion * because in this class, we are attempting to test the ContentProvider. We can't assume * that our ContentProvider's delete method works in our ContentProvider's test class. */ private void deleteAllRecordsFromWeatherTable() { /* Access writable database through WeatherDbHelper */ WeatherDbHelper helper = new WeatherDbHelper(InstrumentationRegistry.getTargetContext()); SQLiteDatabase database = helper.getWritableDatabase(); /* The delete method deletes all of the desired rows from the table, not the table itself */ database.delete(WeatherContract.WeatherEntry.TABLE_NAME, null, null); /* Always close the database when you're through with it */ database.close(); } }
/** * In onCreate, we initialize our content provider on startup. This method is called for all * registered content providers on the application main thread at application launch time. * It must not perform lengthy operations, or application startup will be delayed. * * Nontrivial initialization (such as opening, upgrading, and scanning * databases) should be deferred until the content provider is used (via {@link #query}, * {@link #bulkInsert(Uri, ContentValues[])}, etc). * * Deferred initialization keeps application startup fast, avoids unnecessary work if the * provider turns out not to be needed, and stops database errors (such as a full disk) from * halting application launch. * * @return true if the provider was successfully loaded, false otherwise */ @Override public boolean onCreate() { /* * As noted in the comment above, onCreate is run on the main thread, so performing any * lengthy operations will cause lag in your app. Since WeatherDbHelper's constructor is * very lightweight, we are safe to perform that initialization here. */ mOpenHelper = new WeatherDbHelper(getContext()); return true; }
numRowsDeleted = mOpenHelper.getWritableDatabase().delete( WeatherContract.WeatherEntry.TABLE_NAME, selection,
cursor = mOpenHelper.getReadableDatabase().query( cursor = mOpenHelper.getReadableDatabase().query( WeatherContract.WeatherEntry.TABLE_NAME, projection,
/** * This method will clear all rows from the weather table in our database. * <p> * Please note: * <p> * - This does NOT delete the table itself. We call this method from our @Before annotated * method to clear all records from the database before each test on the ContentProvider. * <p> * - We don't use the ContentProvider's delete functionality to perform this row deletion * because in this class, we are attempting to test the ContentProvider. We can't assume * that our ContentProvider's delete method works in our ContentProvider's test class. */ private void deleteAllRecordsFromWeatherTable() { /* Access writable database through WeatherDbHelper */ WeatherDbHelper helper = new WeatherDbHelper(InstrumentationRegistry.getTargetContext()); SQLiteDatabase database = helper.getWritableDatabase(); /* The delete method deletes all of the desired rows from the table, not the table itself */ database.delete(WeatherContract.WeatherEntry.TABLE_NAME, null, null); /* Always close the database when you're through with it */ database.close(); } }
/** * This database is only a cache for online data, so its upgrade policy is simply to discard * the data and call through to onCreate to recreate the table. Note that this only fires if * you change the version number for your database (in our case, DATABASE_VERSION). It does NOT * depend on the version number for your application found in your app/build.gradle file. If * you want to update the schema without wiping data, commenting out the current body of this * method should be your top priority before modifying this method. * * @param sqLiteDatabase Database that is being upgraded * @param oldVersion The old database version * @param newVersion The new database version */ @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); onCreate(sqLiteDatabase); } }
/** * You do not need to call this method. This is a method specifically to assist the testing * framework in running smoothly. You can read more at: * http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() */ @Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); } }
/** * In onCreate, we initialize our content provider on startup. This method is called for all * registered content providers on the application main thread at application launch time. * It must not perform lengthy operations, or application startup will be delayed. * * Nontrivial initialization (such as opening, upgrading, and scanning * databases) should be deferred until the content provider is used (via {@link #query}, * {@link #bulkInsert(Uri, ContentValues[])}, etc). * * Deferred initialization keeps application startup fast, avoids unnecessary work if the * provider turns out not to be needed, and stops database errors (such as a full disk) from * halting application launch. * * @return true if the provider was successfully loaded, false otherwise */ @Override public boolean onCreate() { /* * As noted in the comment above, onCreate is run on the main thread, so performing any * lengthy operations will cause lag in your app. Since WeatherDbHelper's constructor is * very lightweight, we are safe to perform that initialization here. */ mOpenHelper = new WeatherDbHelper(getContext()); return true; }
numRowsDeleted = mOpenHelper.getWritableDatabase().delete( WeatherContract.WeatherEntry.TABLE_NAME, selection,
cursor = mOpenHelper.getReadableDatabase().query( cursor = mOpenHelper.getReadableDatabase().query( WeatherContract.WeatherEntry.TABLE_NAME, projection,
/** * This method will clear all rows from the weather table in our database. * <p> * Please note: * <p> * - This does NOT delete the table itself. We call this method from our @Before annotated * method to clear all records from the database before each test on the ContentProvider. * <p> * - We don't use the ContentProvider's delete functionality to perform this row deletion * because in this class, we are attempting to test the ContentProvider. We can't assume * that our ContentProvider's delete method works in our ContentProvider's test class. */ private void deleteAllRecordsFromWeatherTable() { /* Access writable database through WeatherDbHelper */ WeatherDbHelper helper = new WeatherDbHelper(InstrumentationRegistry.getTargetContext()); SQLiteDatabase database = helper.getWritableDatabase(); /* The delete method deletes all of the desired rows from the table, not the table itself */ database.delete(WeatherContract.WeatherEntry.TABLE_NAME, null, null); /* Always close the database when you're through with it */ database.close(); } }
/** * This database is only a cache for online data, so its upgrade policy is simply to discard * the data and call through to onCreate to recreate the table. Note that this only fires if * you change the version number for your database (in our case, DATABASE_VERSION). It does NOT * depend on the version number for your application found in your app/build.gradle file. If * you want to update the schema without wiping data, commenting out the current body of this * method should be your top priority before modifying this method. * * @param sqLiteDatabase Database that is being upgraded * @param oldVersion The old database version * @param newVersion The new database version */ @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); onCreate(sqLiteDatabase); } }
/** * You do not need to call this method. This is a method specifically to assist the testing * framework in running smoothly. You can read more at: * http://developer.android.com/reference/android/content/ContentProvider.html#shutdown() */ @Override @TargetApi(11) public void shutdown() { mOpenHelper.close(); super.shutdown(); } }
/** * In onCreate, we initialize our content provider on startup. This method is called for all * registered content providers on the application main thread at application launch time. * It must not perform lengthy operations, or application startup will be delayed. * * Nontrivial initialization (such as opening, upgrading, and scanning * databases) should be deferred until the content provider is used (via {@link #query}, * {@link #bulkInsert(Uri, ContentValues[])}, etc). * * Deferred initialization keeps application startup fast, avoids unnecessary work if the * provider turns out not to be needed, and stops database errors (such as a full disk) from * halting application launch. * * @return true if the provider was successfully loaded, false otherwise */ @Override public boolean onCreate() { /* * As noted in the comment above, onCreate is run on the main thread, so performing any * lengthy operations will cause lag in your app. Since WeatherDbHelper's constructor is * very lightweight, we are safe to perform that initialization here. */ mOpenHelper = new WeatherDbHelper(getContext()); return true; }