Quotes I hope data you want to store in Database and you also required to update these quotes later on.
First, you need to provide implementation for SqliteHelper something like this
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "app_db";
private static final int DB_VERSION = 1;
public DBHelper(@ApplicationContext Application application) {
super(application, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
if (db.isOpen()) {
//Create Quotes Table
QuotesDao.onCreateTable(db);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (db.isOpen()) {
//Upgrade Contacts Table
QuotesDao.onUpgradeTable(db, oldVersion, newVersion);
}
}
When you have done with creating DB now you have a Table Quotes which basically managed in QuotesDao.
QuotesDao is a helper class for Quotes table which provides insert, updated, fetch or delete operation.
Something like this:-
public class QuotesDao {
public static final String TAG = QuotesDao.class.getSimpleName();
public static String QUOTES = "contact_photo_url";
private static final String TABLE_NAME = "quotes";
private static final String[] ALL_COLUMNS = new String[]{
QUOTES,
};
public static void onCreateTable(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS "
+ TABLE_NAME + "("
+ QUOTES + " TEXT"
+ ")";
db.execSQL(sql);
}
public static void onUpgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public synchronized List<Quotes> fetchQuotes(DBHelper dbHelper) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, ALL_COLUMNS, null, null, null, null, null);
//Close the DB when its work is done
List<Quotes> quotes = null;// Parse Cursor object into Quotes list;
return quotes;
}
public synchronized long insert(DBHelper dbHelper, Quotes quotes) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = null;// Map your Object in content values ;
long insert = db.insertWithOnConflict(TABLE_NAME, null
, cv
, SQLiteDatabase.CONFLICT_REPLACE);
return insert;
}
public synchronized long updateQuotes(ContactInfo contactInfo, DBHelper dbHelper) {
//Write Update logic here
return id;
}
/**
* Method to delete a contact from table
* @param contactInfo Contact which need to be deleted
* @param dbHelper DBHelper
*/
public synchronized void delete(ContactInfo contactInfo, DBHelper dbHelper) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
}
/**
* Method to clear all the data from store contact table
* @param dbHelper DBHelper
*/
public void clear(DBHelper dbHelper) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
}
public void onDeleteTable(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME + "'");
}
}
And in future if you need to add more quotes you will insert the quotes in Qutes table using insert method.
And if you required to update the schema of Table then first you need to upgrade the DB version then you will get call in onUpgradeTable() method of DBHelper where you can write upgrade table bussiness logic for more detail how to upgrade table refer here.