This library design to create database using ContentProvider and keeping in mind reduce complexity in database table creation and easy to maintain code.
- To create Database using ContentProvider so that we can share our database to other app if needed.
- Uses ContentProvider in Object Oriented way such that each table have seprate ContentProvider so easy to maintain.
- Developer have to write down very less code to create table so save development time.
- Very simple way to create table creation query String.
- Work with Cursor LoaderManager so no need to close cursor explicitly.
- Use standard android ContentResolver to execute query.
- Library : API 1
- LibrarySample : API 1
1:Create Your Content Provider which will be responsible for table creation. 2:Override all method which is needed as per your requirement. example:
publicclassBookContentProviderextendsEasyDatabaseContentProvider {
publicstaticStringBOOK_TABLE_CREATION_STRING;
publicstaticUriBOOK_TABLE_URI;
publicfinalstaticStringBOOK_TABLE_NAME = "BookTable";
privatefinalintBOOK_TABLE_IDENTIFIER = 101;
// Define column name keypublicstaticfinalStringKEY_BOOK_NAME = "Name";
publicstaticfinalStringKEY_AUTHOR_NAME = "Author";
publicstaticfinalStringKEY_PRICE = "Price";
@Overrideprotectedvoidinitialize() {
// This variable get use in table creation in SQL Helper class because// of that it is staticBOOK_TABLE_CREATION_STRING = getQueryTableString();
// This variable get use in query fire from any where in app because// of that it is staticBOOK_TABLE_URI = getContentURI();
}
@OverridepublicSQLiteOpenHelpergetSqlHelper() {
SQLiteOpenHelpersqlHelper = DemoSqlHelper
.getSQLHelperInstance(getContext());
returnsqlHelper;
}
@OverrideprotectedvoidfillTableParamMap(HashMap<String, String> tableParamMap) {
tableParamMap.put(KEY_ID, "INTEGER PRIMARY KEY autoincrement");
tableParamMap.put(KEY_BOOK_NAME, "varchar(50) not null");
tableParamMap.put(KEY_AUTHOR_NAME, "varchar(50)");
tableParamMap.put(KEY_PRICE, "int");
}
@OverrideprotectedStringgetTableName() {
returnBOOK_TABLE_NAME;
}
@OverrideprotectedintgetTableIdentifier() {
returnBOOK_TABLE_IDENTIFIER;
}
@OverrideprotectedStringgetAuthority() {
returngetContext().getResources().getString(
R.string.book_authority_name);
}
}3:Create Sql Helper class and that should be singleton to improve performance and to avoid multiple data instance creation. example:
publicclassDemoSqlHelperextendsSQLiteOpenHelper {
publicstaticfinalintDATABASE_VERSION = 1;
publicstaticfinalStringDATABASE_NAME = "DemoDatabase";
privatestaticDemoSqlHelpersqlHelperInstance;
publicstaticDemoSqlHelpergetSQLHelperInstance(Contextcontext) {
if (sqlHelperInstance == null) {
sqlHelperInstance = newDemoSqlHelper(context);
}
returnsqlHelperInstance;
}
/** * @param context * This class should be singleton ,to avoid database lock issue. */privateDemoSqlHelper(Contextcontext) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@OverridepublicvoidonCreate(SQLiteDatabasedb) {
db.execSQL(BookContentProvider.BOOK_TABLE_CREATION_STRING);
}
@OverridepublicvoidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {
// TO BE IMPLEMENTED IF DB VERSION INCREMENTED FROM 1;
}
}4:define Content provider inside AndroidMainfest file. example:
<providerandroid:name="com.ajra.demo.database.BookContentProvider"android:authorities="@string/book_authority_name"android:exported="false" />If we would like to share data of table android:exported="true"
5:We are done with table creation.
6:Now use it. example:
ContentValuesvalues = newContentValues();
values.put(BookContentProvider.KEY_BOOK_NAME, bookName);
Uriuri=getContentResolver().insert(BookContentProvider.BOOK_TABLE_URI,
values);No need to add any extra config.
In EasyDatabaseContentProvider class their is method called setDebaugModeOn(boolean isDebaugModeOn) override this method. To grep log use "EasyDatabaseContentProvider" as grep string.
Copyright 2014-present Ajay Sahani
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.