重复列名称lock_status

时间:2016-06-07 16:19:23

标签: android database android-sqlite sqliteopenhelper

我在数据库和onUpgrade()Alter Table中添加了lock_status列,但是当我尝试打开我的应用程序时,它强制停止并出现以下错误。 我应该在onUpgrade()中做什么,我已经使用新列第4次更新了我的数据库。 帮助得到高度赞赏。

 Caused by: android.database.sqlite.SQLiteException: duplicate column name: lock_status (code 1): , while compiling: ALTER TABLE notes ADD COLUMN lock_status INT
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
  at com.revodroid.notes.notes.Adapter.DatabaseHelper.onUpgrade(DatabaseHelper.java:89)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
  at com.revodroid.notes.notes.Adapter.DatabaseHelper.getAllNotes(DatabaseHelper.java:170)
  at com.revodroid.notes.notes.Activity.MainActivity.setupNotesAdapter(MainActivity.java:603)
  at com.revodroid.notes.notes.Activity.MainActivity.onCreate(MainActivity.java:256)
  at android.app.Activity.performCreate(Activity.java:6251)

我的数据库代码:

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String LOG=DatabaseHelper.class.getSimpleName();
private static final int DATABASE_VERSION=4;
public static final String DATABASE_NAME="NotesDB";
public SQLiteDatabase database;

private final Context context;

DatabaseHelper databaseHelper;

public static final String TABLE_NOTES="notes";
public static final String TABLE_PASSCODE = "passcodetable";

public static final String KEY_ID="id";
public static final String KEY_TITLE="title";
public static final String KEY_CONTENT="content";
public static final String KEY_UPDATED_AT="updated_at";
public static final String KEY_COLOR="color";
public static final String KEY_TAG="tag";
public static final String KEY_FAVOURITE="favourite";

public static final String KEY_LOCKSTATUS = "lock_status";
public static final String KEY_PASSCODE_ID="passcode_id";
public static final String KEY_PASSOCDE = "passcode";
public static final String KEY_PASSCODE_STATUS = "passcode_status";


private static final String CREATE_TABLE_NOTE="CREATE TABLE "
        +TABLE_NOTES+"("+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
        +KEY_TITLE+" TEXT, "
        +KEY_CONTENT+" TEXT, "
        +KEY_UPDATED_AT+" BIGINT, "
        +KEY_COLOR+" INT, "
        +KEY_FAVOURITE+" INT, "
        +KEY_LOCKSTATUS+" INT"+" )";

private static final String CREATE_TABLE_PASSCODE="CREATE TABLE IF NOT EXISTS "
        +TABLE_PASSCODE+"("+KEY_PASSCODE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
        +KEY_PASSOCDE+" TEXT"+" )";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_NOTE);
    db.execSQL(CREATE_TABLE_PASSCODE);
}

public void onDowngrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2) {
    onUpgrade(paramSQLiteDatabase, paramInt1, paramInt2);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 3){
        db.execSQL("ALTER TABLE notes ADD COLUMN color INT");
        db.execSQL("ALTER TABLE notes ADD COLUMN favourite INT");
    }
    if (newVersion > oldVersion){
        db.execSQL("ALTER TABLE notes ADD COLUMN lock_status INT");
        db.execSQL("CREATE TABLE IF NOT EXISTS "
                +TABLE_PASSCODE+"("+KEY_PASSCODE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
                +KEY_PASSOCDE+" TEXT"+" )");
    }
}

1 个答案:

答案 0 :(得分:4)

现在编写的代码将尝试在数据库版本增加的每个时间添加列lock_status 。你有一个if语句检查旧版本&lt; 3,然后立即再次检查新版本大于旧版本。当你的版本增加时,第二次检查总是如此,所以它总是会尝试添加列。

假设您在版本4中添加了lock_status列。然后正确的测试将是

if (oldVersion < 4) {
    // your code goes here
}

问题不是(必然)您在创建和升级中添加lock_status,如评论中所建议的那样。如果数据库已存在,则不会调用create方法。如果数据库尚不存在,则不会调用更新方法。