Android Open Source - flashcards Card Db Helper






From Project

Back to project page flashcards.

License

The source code is released under:

MIT License

If you think the Android project flashcards listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.cmu.hcii.ssui.flashcards.db;
/*from  w  w  w  .j  a  v a2 s .c  o m*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import edu.cmu.hcii.ssui.flashcards.db.CardContract.CardTable;
import edu.cmu.hcii.ssui.flashcards.db.CardContract.DeckTable;
import edu.cmu.hcii.ssui.flashcards.db.CardContract.Tables;

public class CardDbHelper extends SQLiteOpenHelper {
    private static final String TAG = CardDbHelper.class.getSimpleName();

    private static final String DATABASE_NAME = "flashcards.db";
    private static final int DATABASE_VERSION = 1;

    /**
     * {@code REFERENCES} clauses.
     * */
    //@formatter:off
    private interface References {
        String DECK_ID = "REFERENCES " + Tables.DECKS + "(" + DeckTable._ID + ") ON DELETE CASCADE";
    }
    //@formatter:on

    //@formatter:off
    private static final String CARD_TABLE_CREATE = "CREATE TABLE " + Tables.CARDS + " ("
            + CardTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + CardTable.DECK_ID + " INTEGER NOT NULL " + References.DECK_ID + ", "
            + CardTable.FRONT + " TEXT, "
            + CardTable.BACK + " TEXT);";
    //@formatter:on

    //@formatter:off
    private static final String DECK_TABLE_CREATE = "CREATE TABLE " + Tables.DECKS + " ("
            + DeckTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + DeckTable.NAME + " TEXT, "
            + DeckTable.DESCRIPTION + " TEXT);";
    //@formatter:on

    private CardDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    private static CardDbHelper sInstance;

    public static CardDbHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new CardDbHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CARD_TABLE_CREATE);
        db.execSQL(DECK_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from "+ oldVersion + " to " + newVersion + ".");

        // Drop older tables if exists.
        db.execSQL("DROP TABLE IF EXISTS " + Tables.CARDS + ";");
        db.execSQL("DROP TABLE IF EXISTS " + Tables.DECKS + ";");

        // Create tables again.
        onCreate(db);
    }

}




Java Source Code List

edu.cmu.hcii.ssui.flashcards.CardListActivity.java
edu.cmu.hcii.ssui.flashcards.Card.java
edu.cmu.hcii.ssui.flashcards.Deck.java
edu.cmu.hcii.ssui.flashcards.StudyActivity.java
edu.cmu.hcii.ssui.flashcards.StudyListActivity.java
edu.cmu.hcii.ssui.flashcards.db.CardContract.java
edu.cmu.hcii.ssui.flashcards.db.CardDatabase.java
edu.cmu.hcii.ssui.flashcards.db.CardDbHelper.java
edu.cmu.hcii.ssui.flashcards.dialogs.DeleteCardDialog.java
edu.cmu.hcii.ssui.flashcards.dialogs.DeleteDeckDialog.java
edu.cmu.hcii.ssui.flashcards.dialogs.EditCardDialog.java
edu.cmu.hcii.ssui.flashcards.dialogs.EditDeckDialog.java
edu.cmu.hcii.ssui.flashcards.dialogs.NewCardDialog.java
edu.cmu.hcii.ssui.flashcards.dialogs.NewDeckDialog.java
edu.cmu.hcii.ssui.flashcards.util.ArgUtil.java