@Override public void onCreate(final SQLiteDatabase db) { Out.d(TAG, "Creation de la BDD"); // we go through the tables creation scripts for (int i = 0; i < CREATE_TABLES.length; i++) { Out.d(TAG, CREATE_TABLES[i]); // we create each table into the database db.execSQL(CREATE_TABLES[i]); } }
/** * Initialize provider context * * @param context is the context of execution. If possible give the Application's context */ public static void initialize(final Context context) { if (sContext == null) { Out.d(TAG, "initialize!!"); sContext = context.getApplicationContext(); sDbHelper = new DatabaseHelper(sContext); } }
/** Codes returned by the URIMatcher corresponding to the URI analyzed */ @Override public boolean onCreate() { Out.d(TAG, "On Create BusinessObjectProvider"); initialize(getContext()); return true; }
@Override public int update( final Uri uri, final ContentValues values, final String where, final String[] whereArgs) { final int match = sUriMatcher.match(uri); // get a readable database access SQLiteDatabase db = sDbHelper.getWritableDatabase(); // log message Out.d(TAG, "BusinessObjectProvider.query: uri=" + uri + ", match is " + match); // we initialize the update counter int count = 0; String table = getTableName(match); // throws an exception if there is no table name if (table == null) throw new IllegalArgumentException("Unknown URI " + uri); // update the database count = db.update(table, values, where, whereArgs); // we notify that a change happened sContext.getContentResolver().notifyChange(uri, null); return count; }
@Override public Cursor query( final Uri uri, final String[] columns, final String where, final String[] whereArgs, final String order) { Cursor c = null; final int match = sUriMatcher.match(uri); // get a readable database access SQLiteDatabase db = sDbHelper.getReadableDatabase(); // log message Out.d(TAG, "BusinessObjectProvider.query: uri=" + uri + ", match is " + match); // we get the table and then query to the database String table = getTableName(match); c = db.query(table, columns, where, whereArgs, null, null, order); // we notify that a change happened if it actually occurred if ((c != null) && !isTemporary()) { c.setNotificationUri(sContext.getContentResolver(), uri); } return c; }
@Override public Uri insert(final Uri uri, ContentValues values) { // we get the type of the URI received final int match = sUriMatcher.match(uri); // log message Out.d(TAG, "BusinessObjectProvider.insert: uri=" + uri + ", match is " + match); // we manage the null ContentValues if (values == null) values = new ContentValues(); // we prepare the database access SQLiteDatabase db = sDbHelper.getWritableDatabase(); // we create the result values long id = BusinessObjectDAO.ID_INVALID; Uri resultUri; // we get the table name and then insert into the database String table = getTableName(match); id = db.insert(table, "foo", values); // we add the id received at the end of the URI resultUri = ContentUris.withAppendedId(uri, id); // we return the URI with the id of the entry at the end return resultUri; }
/** * *************************** * * <p>ContentProvider's Functions * * <p>*************************** */ @Override public int delete(final Uri uri, final String where, final String[] whereArgs) { final int match = sUriMatcher.match(uri); Out.d(TAG, TAG + ".delete: uri=" + uri + ", match is " + match); // We get the database access SQLiteDatabase db = sDbHelper.getWritableDatabase(); int count; // we get the table and then we delete the field into the database String table = getTableName(match); count = db.delete(table, where, whereArgs); sContext.getContentResolver().notifyChange(uri, null); return count; }
@Override public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) { Out.w( TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); // we go through the tables names array for (int i = 0; i < DATABASE_TABLES_NAMES.length; i++) { // we DROP each table db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLES_NAMES[i]); } // we recreate the table onCreate(db); }