2013-05-15 96 views
8

我加入到我的數據庫名爲「MEDIA_TABLE」表中,我得到了以下錯誤代碼:SQLiteException:表已經存在

05-15 20:56:22.976: E/AndroidRuntime(5968): android.database.sqlite.SQLiteException: table personal_stats already exists (code 1): , while compiling: CREATE TABLE personal_stats (_id INTEGER PRIMARY KEY AUTOINCREMENT, pictures TEXT, videos TEXT); 

我的第二個表工作正常。

我試圖卸載我的應用程序並安裝它,它不會工作,也許有數據庫中的表限制?

這裏是我的onCreate代碼:

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_DATE + " TEXT, " + 
      COLUMN_EXERCISE + " TEXT, " + 
      COLUMN_WEIGHT + " TEXT, " + 
      COLUMN_REPS + " TEXT, " + 
      COLUMN_SET_TIME + " TEXT, " + 
      COLUMN_WORKOUT_TIME + " TEXT, " + 
      COLUMN_DISTANCE + " TEXT, " + 
      COLUMN_SPEED + " TEXT, " + 
      COLUMN_REST_TIME + " TEXT, " + 
      COLUMN_NUMBER_OF_SETS + " TEXT, " + 
      COLUMN_FIELDS_PARAMETER + " TEXT, " + 
      COLUMN_NOT_ON_LIST_EXERCISE + " TEXT, " + 
      COLUMN_HERAT_BEAT + " TEXT, " + 
      COLUMN_CALORIES + " TEXT, " + 
      COLUMN_COMMENT + " TEXT);"); 

    db.execSQL("CREATE TABLE " + SECOND_TABLE_NAME + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_AGE + " TEXT, " + 
      COLUMN_AGE_PW + " TEXT, " + 
      COLUMN_GENDER + " TEXT, " + 
      COLUMN_HEIGHT + " TEXT, " + 
      COLUMN_HEIGHT_PW + " TEXT, " + 
      COLUMN_BODY_WEIGHT + " TEXT, " + 
      COLUMN_BODY_WEIGHT_PW + " TEXT, " + 
      COLUMN_PROFILE_PIC + " TEXT, " + 
      COLUMN_FAT + " TEXT, " + 
      COLUMN_FAT_PW + " TEXT);"); 

    db.execSQL("CREATE TABLE " + MEDIA_TABLE + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_PICS + " TEXT, " + 
      COLUMN_VIDEO + " TEXT);"); 

}  

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    db.execSQL("DROP TABLE IF EXISTS " + SECOND_TABLE_NAME); 
    db.execSQL("DROP TABLE IF EXISTS " + MEDIA_TABLE); 
    onCreate(db); 
} 

EDIT : 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_NAME); 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + SECOND_TABLE_NAME); 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + MEDIA_TABLE); 
    onCreate(db); 
} 

回答

33

您應該使用CREATE TABLE IF NOT EXISTS,而不是CREATE TABLE。然後,您的代碼應該是這樣的:

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + TABLE_NAME + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_DATE + " TEXT, " + 
      COLUMN_EXERCISE + " TEXT, " + 
      COLUMN_WEIGHT + " TEXT, " + 
      COLUMN_REPS + " TEXT, " + 
      COLUMN_SET_TIME + " TEXT, " + 
      COLUMN_WORKOUT_TIME + " TEXT, " + 
      COLUMN_DISTANCE + " TEXT, " + 
      COLUMN_SPEED + " TEXT, " + 
      COLUMN_REST_TIME + " TEXT, " + 
      COLUMN_NUMBER_OF_SETS + " TEXT, " + 
      COLUMN_FIELDS_PARAMETER + " TEXT, " + 
      COLUMN_NOT_ON_LIST_EXERCISE + " TEXT, " + 
      COLUMN_HERAT_BEAT + " TEXT, " + 
      COLUMN_CALORIES + " TEXT, " + 
      COLUMN_COMMENT + " TEXT);"); 

    db.execSQL("CREATE TABLE IF NOT EXISTS" + SECOND_TABLE_NAME + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_AGE + " TEXT, " + 
      COLUMN_AGE_PW + " TEXT, " + 
      COLUMN_GENDER + " TEXT, " + 
      COLUMN_HEIGHT + " TEXT, " + 
      COLUMN_HEIGHT_PW + " TEXT, " + 
      COLUMN_BODY_WEIGHT + " TEXT, " + 
      COLUMN_BODY_WEIGHT_PW + " TEXT, " + 
      COLUMN_PROFILE_PIC + " TEXT, " + 
      COLUMN_FAT + " TEXT, " + 
      COLUMN_FAT_PW + " TEXT);"); 

    db.execSQL("CREATE TABLE IF NOT EXISTS" + MEDIA_TABLE + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_PICS + " TEXT, " + 
      COLUMN_VIDEO + " TEXT);"); 

}  

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    db.execSQL("DROP TABLE IF EXISTS " + SECOND_TABLE_NAME); 
    db.execSQL("DROP TABLE IF EXISTS " + MEDIA_TABLE); 
    onCreate(db); 
} 
+0

沒有工作= \ – tomer

+0

你應該改變所有這樣的創建語句。 – Triode

+0

我做到了。看看我的帖子編輯。 – tomer

1

用這種方式

db.execSQL("create table if not exists table_name(rank int, correct_answer int, optionselect int);"; 
+0

didnt worked = \ – tomer

0

你一次又一次地運行相同的程序和相同的表存在於數據庫中。您需要修改您的創建表查詢。 試試這個「創建表如果不存在TABLE_NAME」

+0

didnt worked = \ – tomer