SQLiteException:没有这样的列:DeletedNotes(代码1)

时间:2017-08-26 13:54:12

标签: android sqlite android-sqlite

我有一个名为notes的表和一个名为getlist()的方法,它从数据库中检索数据。现在的问题是,当执行getlist()方法时,应用程序崩溃并且它表示没有这样的列DeletedNotes甚至在create table语句中指定它。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

您应该在列名称DELETEED_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+" "
        +");";