我已经在数据库和onupgrade()alter表中添加了lock_status列,但是当我尝试打开我的应用程序时,它强制停止,并出现以下错误。
我应该在onupgrade()中做什么,我已经用新列第四次更新了数据库。
非常感谢你的帮助。
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"+" )");
}
}
最佳答案
现在编写的代码将在每次数据库版本增加时尝试添加列锁定状态。有一个if语句检查旧版本是否小于3,然后立即有第二个检查新版本是否大于旧版本。当您的版本增加时,第二个检查总是正确的,所以它总是试图添加列。
假设您在版本4中添加了lock_status列。那么正确的测试应该是
if (oldVersion < 4) {
// your code goes here
}
问题不一定是在创建和升级过程中添加了锁状态,如注释中所建议的那样。如果数据库已经存在,将不调用CREATE方法。如果数据库还不存在,则不调用更新方法。