-2

I have a Table named as notes and a method called getlist() which retrieve data from the database. Now the problem is when getlist() method is executed the application crashes and it says that no such column DeletedNotes even tho it is specified in create table statement.

Here is my code:

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;
}

and here is my 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: Force finishing activity com.example.chirag.stickynotes/.MainActivity 08-26 18:52:15.121 1585-3167/system_process I/OpenGLRenderer: Initialized EGL, version 1.4 08-26 18:52:15.121 1585-3167/system_process D/OpenGLRenderer: Swap behavior 1

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Chirag Parekh
  • 102
  • 2
  • 7

1 Answers1

0

You should leave space beside the column name, DELETED_NOtes and Archived_Notes. In your code, since you don't have space between the column name and the type INTEGER, the table would have got created with a column name like this: 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+" "
        +");";
Bob
  • 13,447
  • 7
  • 35
  • 45