0

I'm developing a Android app, in which stores quotes. Tell me please, how add new quotes in database in future?

I wish what would it be convenient, for example a separate activity where I write a quote, author, subject, and by clicking that would have this quote was added to the database. But what would such users could not do.

I plan to use Firebase realtime database, but how to organize update on the side of the administrator, I do not know. Perhaps I need to write a separate application to work with the database? Thanks in advance.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • you can just create a speical quote-adding-activity for logged in users who are administrators, and you can enforce that with firebase security rules. – Linxy Feb 22 '17 at 08:45

2 Answers2

0

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.

Community
  • 1
  • 1
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0

For storing quotes, you need to implement Screen where user can add new quotes. For storing that object you need to create SQLite database(for persistent storage).

But your usecase is, you want to manage that database also. So, for that SQLite doesn't helps. For that usecase you need to store data on Remote Server. From there only you can manage that database.

Firebase is a very good option for that, there you get some extra features also.

Ashish M
  • 763
  • 6
  • 13
  • How do I interact with the database? From my application? Then how to make what people can not see the additional functionality? Write a separate application to work with the database? – Bogdan Martynov Feb 22 '17 at 08:06
  • - Here is comlete guide for firebase [Firebase Guide](https://firebase.google.com/docs/database/). - For making functionality depending on user, you can save user type and according to that you can show or hide fuctionality. – Ashish M Feb 22 '17 at 12:22