Database Manager Guide
The Oracle CX Core SDK provides a database layer to deal with CRUD operations. The provided database layer is the wrapper around android SQLite database. All the database operations can be handled with the class ORADBManager
Creating the database
ORADBManager is an abstract class. You can subclass by implementing onCreateDB and onUpgradeDB to create & modify database with tables.
onCreateDB is the method where you can create the structure of the tables. This method will be called only once. You can write the table creation statements here. You can create multiple tables here. To create tables you need to pass the valid sqlite create table query. Below is the sample code.
To access the database instantiate your subclass of ORADBManager with the help of any one of the provided constructor.
The subclass will look like below.
Note: Refer Upgrading the database section for databaseVersion info in the constructor.
Access the database / tables
ORADBManager provides number of concrete methods to do read and write operations on the tables.
Insert
The above method will insert a new row in to the table or replace the existing row based on primary key constraint.
Retrieve the data
getData is the method to fetch the table rows. There are multiple versions of getData method are available.
-
publicsynchronized List<ContentValues> getData(String tableName, int count)
The above method will return top
countnumber of rows from the table. -
publicsynchronized List<ContentValues> getData(String tableName, String whereColumn, String whereArg)
The above method return rows which are matching for the
wherecriteria. Below is the example code.dbMgr.getData("tableName", "colName", "value");The above will return the all the rows with
colNamecontainsvalue -
publicsynchronized List<ContentValues> getData(String tableName, String colName, String[] range)
The above will return the rows between the rage. We need to pass array contains two values. Assume array contains
n1andn2, it will return all the rows with column value in betweenn1andn2. -
publicsynchronized List<ContentValues> getData(String query)
The above method will return the table data based on the query. The query will any valid sqlite
selectquery. This method is use to get table data from multiple tables.
Update data
To update the record, below is the method, which accepts string query as a parameter and returns true if record(s) updated successfully, else false
Delete rows
deleteData is the method to delete the table rows. There are multiple versions of deleteData method are available.
-
publicsynchronizedboolean deleteData(String tableName, String colName, String value)
The above method delete all the rows in which
colNamehasvalue. -
publicsynchronizedboolean deleteData(String tableName, String colName, String[] values)
The above method delete all the rows in which
colNamehas the values. -
publicsynchronizedboolean deleteData(String tableName)
This method will remove all the rows from the table.
-
publicsynchronizedboolean deleteDataByQuery(String sqlQuery)
The above method will delete the table data based on the query. The query will any valid sqlite
deletequery.
Upgrading the database
Once you created database with tables, there may be any requirement to remove or add new tables, or need to alter the table structure. onUpgradeDB method will be used in such scenarios. Here database version will play a major role in upgrading the database.
If you see ORADBManager constructor, it has a parameter called databaseVersion. Every time you initiate ORADBManager, you have to pass the correct database version.
Assume at the time of database creation you passed the value as 1 for databaseVersion. So every time you access the ORADBManager you have to pass 1 only.
Now to upgrade the database you need to write the update queries in onUpgrade method.
If you see onUpgrade signature it contains two integer values, oldVersion and newVersion. So when you implement onUpgrade method, to invoke this method, you have to pass new value to the ORADBManager constructor's databaseVersion. And this value should be higher than earlier one. So the onUpgrade method's oldVersion will take earlier value, and newVersion will take latest value. For the subsequence ORADBManager instantiation, you have to pass new value only.
Note: The onUpgrade will be called only when you pass new higher value to the ORADBManager constructor. Otherwise it won't be called.
Below is the sample code if you want to add new column to contacts table which is shown in "Creating the database" example