SQLLite Open Helper - 列不存在

时间:2016-05-02 11:54:55

标签: android sqlite

我的Android应用程序中有SQLlite DB的问题。我有DatabaseHelper类,你可以看到如下。我在一个活动中称之为简单插入。但我得到异常表tvSeries没有名为last_episode_id的列。我几乎看起来不会创建一张桌子?但我看不出问题。

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);
 }

1 个答案:

答案 0 :(得分:3)

KEY_ID + "INTEGER PRIMARY KEY, " +
KEY_LAST_ID + "INTEGER, " +
KEY_LAST_NAME + "TEXT, " +

您的列名称及其类型之间需要空格。

修好后,make sure the database schema gets updated