2017-08-26 91 views
-2

我有一個名爲註釋的表和一個名爲getlist()的方法,它從數據庫中檢索數據。現在的問題是,執行getlist()方法時應用程序崩潰,並且它說沒有這樣的列DeletedNotes,即使它是在create table語句中指定的。SQLiteException:沒有這樣的列:DeletedNotes(code 1)

這裏是我的代碼:

public class DBOpenHelper extends SQLiteOpenHelper { 



public static final String DB_Name="Notes.db"; 
public static final int DB_Version=1; 

public static final String Table_Name="NoteData"; 
public static final String id="_id"; 
public static final String Note_Title="noteTitle"; 
public static final String Note_Text="NoteText"; 
public static final String Note_Created="NoteCreated"; 
public static final String DELETED_NOtes="DeletedNotes"; 
public static final String Archived_Notes="ArchiveNotes"; 
public static final int True=1; 
public static final int False=0; 




//SQLite Query 

public static final String Table_Create= 
     "CREATE TABLE "+Table_Name+"(" 
     +id+" INTEGER PRIMARY KEY, " 
     +Note_Title+" TEXT, " 
     +Note_Text+" TEXT, " 
     +Note_Created+" TEXT default CURRENT_TIMESTAMP, " 
     +DELETED_NOtes+"INTEGER DEFAULT "+False+", " 
     +Archived_Notes+"INTEGER DEFAULT "+False+" " 
     +");"; 


public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, DB_Name, null, DB_Version); 
} 

@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 
    sqLiteDatabase.execSQL(Table_Create); 
} 

@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+Table_Name); 
    onCreate(sqLiteDatabase); 

} 


public Cursor getList(){ 
    SQLiteDatabase sqLiteDatabase=this.getWritableDatabase(); 
    Cursor data=sqLiteDatabase.rawQuery("Select * from "+Table_Name+" WHERE "+DELETED_NOtes+" = "+False+" AND "+Archived_Notes+" = "+False+"; ",null); 
    return data; 
} 

這裏是我的logcat:

08-26 18:52:14.812 8731-8731/com.example.chirag.stickynotes E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.chirag.stickynotes, PID: 8731 
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chirag.stickynotes/com.example.chirag.stickynotes.MainActivity}: android.database.sqlite.SQLiteException: no such column: DeletedNotes (code 1): , while compiling: Select * from NoteData WHERE DeletedNotes = 0 AND ArchiveNotes = 0; 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:154) 
                       at android.app.ActivityThread.main(ActivityThread.java:6119) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                      Caused by: android.database.sqlite.SQLiteException: no such column: DeletedNotes (code 1): , while compiling: Select * from NoteData WHERE DeletedNotes = 0 AND ArchiveNotes = 0; 
                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
                       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:1318) 
                       at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257) 
                       at com.example.chirag.stickynotes.DBOpenHelper.getList(DBOpenHelper.java:85) 
                       at com.example.chirag.stickynotes.TextFragment.onCreateView(TextFragment.java:64) 
                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239) 
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332) 
                       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574) 
                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641) 
                       at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794) 
                       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415) 
                       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200) 
                       at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153) 
                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063) 
                       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
                       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554) 
                       at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
                       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248) 
                       at android.app.Activity.performStart(Activity.java:6696) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
                       at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:154)  
                       at android.app.ActivityThread.main(ActivityThread.java:6119)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

08-26 18:52:14.893 1585年至1662年/ system_process W/ActivityManager:強制完成活動COM example.chirag.stickynotes/.MainActivity 08-26 18:52:15.121 1585-3167/system_process I/OpenGLRenderer:初始化EGL,版本1.4 08-26 18:52:15.121 1585-3167/system_process D/OpenGLRenderer:交換行爲1

回答

0

您應該在列名稱DELETED_NOtes和Archived_Notes旁邊留出空格。在你的代碼,因爲你沒有列名和類型INTEGER之間的空間,該表會得到一個列名創建這樣的:DeletedNotesINTEGER

public static final String Table_Create = 
     "CREATE TABLE "+Table_Name +"(" 
     +id+" INTEGER PRIMARY KEY, " 
     +Note_Title+" TEXT, " 
     +Note_Text+" TEXT, " 
     +Note_Created+" TEXT default CURRENT_TIMESTAMP, " 
     +DELETED_NOtes+" INTEGER DEFAULT "+False+", " 
     +Archived_Notes+" INTEGER DEFAULT "+False+" " 
     +");"; 
相關問題