0
public class taskDBHelper extends SQLiteOpenHelper {

//To help with identifying log messages
public static final String LOG_TAG = taskDBHelper.class.getSimpleName();

/** Name of the database file */
private static final String DATABASE_NAME = "taskDataBase.db";
/**
 * Database version. If you change the database schema, you must increment the database version.
 */
private static final int DATABASE_VERSION = 1;


/**
 * Constructs a new instance of {@link taskDBHelper}
 * calls the SQLiteOpenHelper database superclass.
 *
 * @param context that the app is in
 */
public taskDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**This is called when the database is created for the very first time
 * @param db
 */
@Override
public void onCreate(SQLiteDatabase db) {
    //This string is used to create the table for tasks
    String SQL_CREATE_TASK_DATABASE = "CREATE TABLE " + taskContract.TaskEntry.TABLE_NAME + " ("
            + taskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + taskContract.TaskEntry.COLUMN_TASK_TITLE + " TEXT NOT NULL, "
            + taskContract.TaskEntry.COLUMN_DESCRIPTION + " TEXT, "
            + taskContract.TaskEntry.COLUMN_DATE + " INTEGER DEFAULT 0, "
            + taskContract.TaskEntry.COLUMN_LAST_COMPLETED + " INTEGER DEFAULT 0, "
            + taskContract.TaskEntry.COLUMN_TIME + " TEXT NOT NULL DEFAULT 0, "
            + taskContract.TaskEntry.COLUMN_RECCURING_PERIOD + " INTEGER NOT NULL DEFAULT 0, "
            + taskContract.TaskEntry.COLUMN_HISTORY + " TEXT, "
            + taskContract.TaskEntry.COLUMN_TYPE_TASK + " INTEGER NOT NULL, "
            + taskContract.TaskEntry.COLUMN_STATUS + " INTEGER NOT NULL)";
    db.execSQL(SQL_CREATE_TASK_DATABASE);


    //This String creates table for

    String SQL_CREATE_Label_DATABASE = "CREATE TABLE " + taskContract.TaskEntry.LABEL_TABLE_NAME + " ("
            + taskContract.TaskEntry.Label_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + taskContract.TaskEntry.COLUMN_LABEL_NAME + " TEXT NOT NULL)";
    db.execSQL(SQL_CREATE_Label_DATABASE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //not used
    //onCreate(db);
    db.execSQL("DROP TABLE IF EXISTS "+ taskContract.TaskEntry.TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS "+taskContract.TaskEntry.LABEL_TABLE_NAME);
    onCreate(db);
}

}

The first table is created and works as intended but the second table is not executed, I have tried ending both statements with semicolons, but that didn't help. Anytime I try and access the second table, it tells me the table does not exist. Any help would be greatly appreciated, thanks :)

    taskDBHelper mDbHelper = new taskDBHelper(this);
    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(taskContract.TaskEntry.COLUMN_LABEL_NAME, newTextLabel);

    db.insert(taskContract.TaskEntry.LABEL_TABLE_NAME,null,cv);
John
  • 137
  • 10

1 Answers1

1

Your code looks good, often a common error when working with SQlite databases is upon re-constructing them. If you make any changes to the structure of your database (or changes to the data in the database for that matter), you should UNINSTALL the app from your device or emulator then try to run the program again. This will destroy and recreate your database with the changes.

I used to assume that running the app automatically would destroy and recreate my database, but that is no the case.

Also, Another quick "Hack" is to change your databases name to null. //You Database Helper Constructor public DatabaseHelper(Context myContext){ //Context,DatabaseName,CursorFactory,DatabaseVersion super(myContext,null,null,myDatabaseVersionNumber);

Doing this will cause the Database to destroy and re-create itself from squre 1 every-time you launch your application (or press the "play button" in your IDE). This is a great "hack" while coding and testing out your database, but do remember to set a name to your database later whenever you want it to act like a database (that is to actually store info and re-use stored values)!

Paris B. G.
  • 374
  • 1
  • 3
  • 14