i have a problem with SQLlite DB in my android app. I have DatabaseHelper class, as you can see bellow. And i am calling simple insert in an activity. But i am getting exception table tvSeries has no column named last_episode_id. I almost looks like it wont create a table ? But i cant see a problem.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tvSeries";
private static final String TVSERIE_TABLE_NAME = "tvSeries";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_LAST_ID = "last_episode_id";
private static final String KEY_LAST_NAME = "last_episode_name";
private static final String TVSERIE_TABLE_CREATE =
"CREATE TABLE " + TVSERIE_TABLE_NAME + " (" +
KEY_ID + "INTEGER PRIMARY KEY, " +
KEY_LAST_ID + "INTEGER, " +
KEY_LAST_NAME + "TEXT, " +
KEY_NAME + " TEXT);";
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TVSERIE_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TVSERIE_TABLE_NAME);
// create new tables
onCreate(db);
}
public long createRecord(TvSerie serie){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, serie.getId());
values.put(KEY_NAME, serie.getName());
values.put(KEY_LAST_ID, serie.getLastEpisodeId());
values.put(KEY_LAST_NAME, serie.getLastEpisodeName());
// insert row
long id = db.insert(TVSERIE_TABLE_NAME, null, values);
return id;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TvSerie tvSerieTest = new TvSerie();
tvSerieTest.setName("Test");
tvSerieTest.setId(1);
DatabaseHelper db ;
db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
db.createRecord(tvSerieTest);
}
Aucun commentaire:
Enregistrer un commentaire