2015-11-20 96 views
0

我試圖插入一個新的列在我的數據庫..通過改變數據庫...並且它工作正常,如果在數據庫中有以前的數據...但如果應用程序是新安裝的,並試圖建立第一個數據則顯示「表中有沒有這樣的列錯誤(代碼1): 請人幫助與解決方案:
CODE:表沒有列(代碼1)錯誤Android

public class DatabaseHelper extends SQLiteOpenHelper { 

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

    public static final String TABLE_NOTES="notes"; 
    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"; 

/*private static final String DATABASE_ALTER_COLOR = "ALTER TABLE " 
     + TABLE_NOTES + " ADD COLUMN " + KEY_COLOR + " INT"; 

private static final String DATABASE_ALTER_COLOR_1 = "ALTER TABLE " 
     + TABLE_NOTES + " ADD COLUMN " + KEY_COLOR + " INTEGER";*/ 

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 "+")"; 


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

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

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 < 2){ 
     //final String ALTER_TABLE = "ALTER TABLE " + TABLE_NOTES + 
      //  " ADD COLUMN "+ KEY_COLOR+ " INT not null;"; 
     final String DATABASE_ALTER_COLOR = "ALTER TABLE " 
       + TABLE_NOTES + " ADD COLUMN " + KEY_COLOR + " INTEGER"; 
     db.execSQL(DATABASE_ALTER_COLOR); 
    }/*else { 
     db.execSQL("DROP TABLE IF EXIST " + TABLE_NOTES); 
     onCreate(db); 
    }*/ 
} 

public long createNote(Note note){ 
    SQLiteDatabase db=this.getWritableDatabase(); 
    ContentValues values=new ContentValues(); 
    values.put(KEY_TITLE, note.getTitle()); 
    values.put(KEY_CONTENT, note.getContent()); 
    values.put(KEY_UPDATED_AT, note.getUpdatedAt().getTime()); 
    values.put(KEY_COLOR,note.getColor()); 
    return db.insert(TABLE_NOTES, null, values); 
} 

public List<Note> getAllNotes(){ 
    List<Note> result=new ArrayList<>(); 
    SQLiteDatabase db=this.getReadableDatabase(); 
    String selectQuery="SELECT * FROM "+TABLE_NOTES; 
    Log.e(LOG, selectQuery); 
    Cursor cursor=db.rawQuery(selectQuery, null); 
    if (cursor.moveToFirst()){ 
     do { 
      Note note=new Note(); 
      note.setId(cursor.getLong(cursor.getColumnIndex(KEY_ID))); 
      note.setTitle(cursor.getString(cursor.getColumnIndex(KEY_TITLE))); 
      note.setContent(cursor.getString(cursor.getColumnIndex(KEY_CONTENT))); 
      note.setUpdatedAt(new Date(cursor.getLong(cursor.getColumnIndex(KEY_UPDATED_AT)))); 
      note.setColor(cursor.getInt(cursor.getColumnIndex(KEY_COLOR))); 
      result.add(note); 
     }while (cursor.moveToNext()); 
    } 
    return result; 
} 

public void updateNote(Note note){ 
    SQLiteDatabase db=this.getWritableDatabase(); 
    ContentValues values=new ContentValues(); 
    values.put(KEY_TITLE, note.getTitle()); 
    values.put(KEY_CONTENT, note.getContent()); 
    values.put(KEY_UPDATED_AT, note.getUpdatedAt().getTime()); 
    values.put(KEY_COLOR,note.getColor()); 
    db.update(TABLE_NOTES, values, KEY_ID + " = ?", new String[]{String.valueOf(note.getId())}); 
} 

LOG:

table notes has no column named color 
android.database.sqlite.SQLiteException: table notes has no column named color (code 1): , while compiling: INSERT INTO notes(updated_at,title,content,color) VALUES (?,?,?,?) 
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891) 
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502) 
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.insertWithOnConflict(SQLiteDatabase.java:1469) 
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) 
at com.revodroid.notes.notes.Adapter.DatabaseHelper.createNote(DatabaseHelper.java:81) 
at com.revodroid.notes.notes.Activity.MainActivity.addNote(MainActivity.java:855) 
at com.revodroid.notes.notes.Activity.MainActivity.onActivityResult(MainActivity.java:846) 
at android.app.Activity.dispatchActivityResult(Activity.java:6192) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:3603) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650) 
at android.app.ActivityThread.access$1400(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 

如何在列表中插入列而不會丟失數據庫中的先前數據? 幫助非常高度讚賞.. !!!

+1

增加您的DATABASE_VERSION常數值並刪除'onUpgrade()'中的if條件。 –

+0

@ FrankN.Stein我已經將數據庫版本從1增加到2,我將通過刪除條件再次嘗試! –

+0

重新檢查您的查詢,他們是否正確或不... – Sayem

回答

1

檢查您的「CREATE_TABLE」字符串。您在實際的「創建」查詢中未包含KEY_COLOR。您只將其定義爲全局變量。

+0

我用Alter Table KEY_COLOR在onUpgrade()方法中提供了它。我還必須將它添加到CREATE_TABLE中嗎? –

+0

@PrasadShirvandkar是的,您需要這樣做。 –

+0

@NigamPatro如果他正在爲其數據庫的新字段升級生產應用程序,我強烈建議不要只更改create語句。那麼使用'onUpgrade'方法並使用alter語句更爲合適。 @PrasadShirvandkar如果這只是一個新的應用程序,尚未上傳到Google Play商店,那麼請務必刪除當前的應用程序並重新創建應用程序。只要確保你真的刪除了應用程序或刪除了數據,因爲'onCreate'只會在你第一次安裝應用程序時被調用。 – Darwind