0

I have the database schema as:

public class Database extends SQLiteOpenHelper {

    public static final String TAG = Database.class.getSimpleName();

    public static final String DATABASE_NAME = "Database";
    public static final int DATABASE_VERSION = 1;

    SaveSharedPreference preference = new SaveSharedPreference();


    public Database(Context context) {
        super(context, Environment.getExternalStorageDirectory() + "/sdcard/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

    static SQLiteDatabase database = null;
    static Database instance = null;

    public static SQLiteDatabase getDatabase() {
        if (null == database) {
            database = instance.getWritableDatabase();
        }
        return database;
    }

    public static void init(Context context) {
        if (null == instance) {
            instance = new Database(context);
        }
    }

    public static void deactivate() {
        if (null != database && database.isOpen()) {
            database.close();
        }
        database = null;
        instance = null;
    }

    // NON-CUSTOM COLUMNS
    public static final String _ID = "id";
    public static final String CREATE_DT = "create_dt";
    public static final String UPDATE_DT = "update_dt";

    // EVENT TABLE
    public static final String TABLE_NAME_EVENTS = "events";
    public static final String EVENT_SYNC_ID = "event_sync_id";
    public static final String EVENT_CREATOR_SYNC_ID = "event_creator_sync_id";
    public static final String EVENT_PREFERENCES = "event_preferences";


    public static final String TABLE_EVENTS_CREATE = "CREATE TABLE "
            + TABLE_NAME_EVENTS + " ("
            + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + EVENT_SYNC_ID + " VARCHAR(255) NOT NULL, "
            + EVENT_CREATOR_SYNC_ID + " VARCHAR(255) NOT NULL, "
            + EVENT_PREFERENCES + " VARCHAR(8000) NOT NULL, "
            + CREATE_DT + " VARCHAR(255), "
            + UPDATE_DT + " VARCHAR(255))";

    // DROPPING TABLE:  Should do instead of truncate if need to refresh all data/
    public static final String DROP_TABLE_PREFIX = "DROP TABLE IF EXISTS ";

    @Override
    public void onCreate(SQLiteDatabase db) {

        // CREATE TABLES DEFINED ABOVE
        db.execSQL(DROP_TABLE_PREFIX + TABLE_NAME_EVENTS);

        onCreate(db);
    }

and MyApplicaton class, to create the db on app start:

public void onCreate() {
    super.onCreate();

    Database DB = new Database(this);
    SQLiteDatabase db = DB.getWritableDatabase();

}

I have attempted using the code outlined above, yet looking at the file path /sdcard/, there is no database created.

How can this database be created and ensure that it is created?

2
  • you are going to see your database on device or emulator?? Commented Mar 8, 2016 at 5:12
  • I can't see an execSQL() for your TABLE_EVENTS_CREATE in the onCreate() method of your Database class. Does it exist in your actual code? Without it, the database will not be created. Commented Mar 8, 2016 at 5:30

1 Answer 1

1
You can only create database in onCreate() Method

@Override
    public void onCreate(SQLiteDatabase db) {

        // CREATE TABLES DEFINED ABOVE
        db.execSQL(TABLE_NAME_EVENTS);

        onCreate(db);
    }

for Dropping table use 

@Override
    public void onUpgrade(SQLiteDatabase db) {

        // DroppingTABLES DEFINED ABOVE
        db.execSQL(DROP_TABLE_PREFIX );

        onCreate(db);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.