lundi 22 décembre 2014

Table is not being created in SQLite database in Android (No such table...)

I'm using two SQLiteHelper classes to create two different tables in the database: one for logs and one for courses. I've used the on for courses already and it works fine, but the other one doesn't want to create a table in the database. When I use the insert method to insert into the logs table, it doesn't give any errors, but when I trie to read it, it gives the "No such table" error. Then when I check which tables are in the database, only the courses tables exists.


Here is the course SQLiteHelper class:



public class SQLiteHelperCourse extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

public SQLiteHelperCourse(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ CourseEntry.TABLE_NAME + " (" + CourseEntry._ID
+ " INTEGER PRIMARY KEY, " + CourseEntry.COLUMN_NAME_COURSE_ID
+ TEXT_TYPE + COMMA_SEP + CourseEntry.COLUMN_NAME_COURSE_NAME
+ TEXT_TYPE + COMMA_SEP + CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE
+ COMMA_SEP + CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE
+ COMMA_SEP + CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
+ CourseEntry.TABLE_NAME;

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}

}


The log SQLiteHelper class:



public class SQLiteHelperLog extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

public SQLiteHelperLog(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ LogEntry.TABLE_NAME + " (" + LogEntry._ID
+ " INTEGER PRIMARY KEY, " + LogEntry.COLUMN_NAME_COURSE
+ TEXT_TYPE + COMMA_SEP + LogEntry.COLUMN_NAME_START + TEXT_TYPE
+ COMMA_SEP + LogEntry.COLUMN_NAME_END + TEXT_TYPE + COMMA_SEP
+ LogEntry.COLUMN_NAME_DURATION + TEXT_TYPE + COMMA_SEP
+ LogEntry.COLUMN_NAME_COMMENT + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
+ LogEntry.TABLE_NAME;

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}

}

Aucun commentaire:

Enregistrer un commentaire