2016-08-09 84 views
1

我在備忘錄應用程序中使用SQLite數據庫,它工作正常。我想補充另一列對筆記的標題,這是我的新代碼的結果:將另一列添加到SQLite數據庫Android

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

// class to create the sqlite database 
public class DBOpenHelper extends SQLiteOpenHelper { 

    // database name and version 
    // name needs .db 
    private static final String DATABASE_NAME = "notes.db"; 
    private static final int DATABASE_VERSION = 1; 

    // database table and columns 
    // id will be primary key and content provider will expect it to have "_" in front 
    public static final String TABLE_NOTES = "notes"; 
    public static final String NOTE_ID = "_id"; 
    public static final String NOTE_TITLE = "noteTitle"; 
    public static final String NOTE_BODY = "noteBody"; 
    public static final String NOTE_CREATED = "noteCreated"; 

    public static final String[] ALL_COLUMNS = 
      {NOTE_ID, NOTE_TITLE, NOTE_BODY, NOTE_CREATED}; 
    // SQL to create table 
    private static final String TABLE_CREATE = 
      "CREATE TABLE " + TABLE_NOTES + " (" + 
        NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        NOTE_TITLE + " TEXT, " + 
        NOTE_BODY + " TEXT, " + 
        NOTE_CREATED + " TEXT default CURRENT_TIMESTAMP" + 
        ")"; 

    // constructor 
    public DBOpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // called the first time this class is instantiated 
    // creates database structure if it does not exist yet 
    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     sqLiteDatabase.execSQL(TABLE_CREATE); 
    } 

    // called when database version is changed and 
    // user opens app first time after database updated 
    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 
     sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NOTES); 
     onCreate(sqLiteDatabase); 
    } 
} 

這是我的舊代碼(沒有太大改變)。我所做的只是一列中添加對NOTE_TITLE:

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

// class to create the sqlite database 
public class DBOpenHelper extends SQLiteOpenHelper { 

    // database name and version 
    // name needs .db 
    private static final String DATABASE_NAME = "notes.db"; 
    private static final int DATABASE_VERSION = 1; 

    // database table and columns 
    // id will be primary key and content provider will expect it to have "_" in front 
    public static final String TABLE_NOTES = "notes"; 
    public static final String NOTE_ID = "_id"; 
    public static final String NOTE_BODY = "noteBody"; 
    public static final String NOTE_CREATED = "noteCreated"; 

    public static final String[] ALL_COLUMNS = 
      {NOTE_ID, NOTE_BODY, NOTE_CREATED}; 
    // SQL to create table 
    private static final String TABLE_CREATE = 
      "CREATE TABLE " + TABLE_NOTES + " (" + 
        NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        NOTE_BODY + " TEXT, " + 
        NOTE_CREATED + " TEXT default CURRENT_TIMESTAMP" + 
        ")"; 

    // constructor 
    public DBOpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // called the first time this class is instantiated 
    // creates database structure if it does not exist yet 
    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     sqLiteDatabase.execSQL(TABLE_CREATE); 
    } 

    // called when database version is changed and 
    // user opens app first time after database updated 
    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 
     sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NOTES); 
     onCreate(sqLiteDatabase); 
    } 
} 

這都是我的錯誤,我得到的,當我嘗試運行此:

08-09 19:13:19.561 10539-10539/com.sample.mynote I/art: Late-enabling -Xcheck:jni 08-09 19:13:19.592 10539-10539/com.sample.mynote I/HyLog: I : openReadStream, /data/font/config/sfconfig.dat, case (2) 08-09 19:13:19.592 10539-10539/com.sample.mynote D/HyLog: D: Wrong tag (927 : loadPreData() : frameworks/base/core/jni/android/graphics/TypefaceHyFontManager.cpp) 08-09 19:13:19.592 10539-10539/com.beauty.sample.mynote I/HyLog: I : openReadStream, /data/font/config/sfconfig.dat, case (2) 08-09 19:13:19.605 10539-10539/com.beauty.sample.mynote W/System: ClassLoader referenced unknown path: /data/app/com.sample.mynote-1/lib/arm64 08-09 19:13:19.882 10539-10539/com.sample.mynote W/System: ClassLoader referenced unknown path: /data/app/com.sample.mynote-1/lib/arm64 08-09 19:13:19.997 10539-10539/com.sample.mynote W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 08-09 19:13:20.012 10539-10539/com.beauty.sample.mynote I/PhoneWindow: [generateLayout] setColorNavigationBar => color=0x ff000001 08-09 19:13:20.029 10539-10539/com.sample.mynote D/PhoneWindowEx: Ex2. SystemProperties.get result >> #ff000000 08-09 19:13:20.029 10539-10539/com.sample.mynote D/PhoneWindowEx: [PWEx][generateLayout] setNavigationBarColor2 : colors=0xff000000 08-09 19:13:20.029 10539-10539/com.sample.mynote I/PhoneWindow: [setNavigationBarColor2] color=0x ff000000 08-09 19:13:20.137 10539-10586/com.sample.mynote E/SQLiteLog: (1) no such column: noteTitle 


--------- beginning of crash 08-09 19:13:20.139 10539-10586/com.sample.mynote E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1 
                       Process: com.sample.mynote, PID: 10539 
                       java.lang.RuntimeException: An error occurred while executing doInBackground() 
                        at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:143) 
                        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
                        at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
                        at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                        at java.lang.Thread.run(Thread.java:818) 
                       Caused by: android.database.sqlite.SQLiteException: no such column: noteTitle (code 1): , while compiling: SELECT _id, noteTitle, noteBody, noteCreated FROM notes ORDER BY noteCreated DESC 
                        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893) 
                        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504) 
                        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726) 
                        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
                        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
                        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
                        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1426) 
                        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1273) 
                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1144) 
                        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1312) 
                        at com.beauty.comp_eng.mynote.NoteProvider.query(NoteProvider.java:62) 
                        at android.content.ContentProvider.query(ContentProvider.java:1017) 
                        at android.content.ContentProvider$Transport.query(ContentProvider.java:238) 
                        at android.content.ContentResolver.query(ContentResolver.java:498) 
                        at android.support.v4.content.ContentResolverCompatJellybean.query(ContentResolverCompatJellybean.java:29) 
                        at android.support.v4.content.ContentResolverCompat$ContentResolverCompatImplJB.query(ContentResolverCompat.java:57) 
                        at android.support.v4.content.ContentResolverCompat.query(ContentResolverCompat.java:125) 
                        at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:59) 
                        at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:37) 
                        at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:296) 
                        at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:54) 
                        at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:42) 
                        at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:128) 
                        at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
                        at java.lang.Thread.run(Thread.java:818)  08-09 19:13:20.153 10539-10591/com.sample.mynote D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 08-09 19:13:20.474 10539-10591/com.sample.mynote I/Adreno: QUALCOMM build  : ac1ef73, I86756fd4a8 
                     Build Date      : 10/05/15 
                     OpenGL ES Shader Compiler Version: XE031.06.00.00 
                     Local Branch      : 
                     Remote Branch     : 
                     Remote Branch     : 
                     Reconstruct Branch    : 08-09 19:13:20.483 10539-10591/com.sample.mynote I/OpenGLRenderer: Initialized EGL, version 1.4 08-09 19:13:20.605 10539-10539/com.sample.mynote V/ViewRootImpl: Contents drawing finished : com.sample.mynote/com.sample.mynote.MainActivity 

如何添加列的筆記標題到這個應用程序?

回答

2

你需要增加你的數據庫版本,每次你改變數據庫。 private static final int DATABASE_VERSION = 1;應更改爲private static final int DATABASE_VERSION = 2;

相關問題