0

not able to retrieve data from cursor method in database into my simplecursor adapter in homepage in application. i tried my level best and i did all the researches in stack overflow and other sites also but not able to get the expected solution

//this is the table i created for storing cities data

  public static final String City="Citi";
    public static final String rowid="_id";
    private static final int DATABASE_VERSION=8;
    SQLiteDatabase db;
    private Context context;

    //Creating Cities Database table
    public static final String CreateCities="CREATE TABLE IF NOT EXISTS "+Citietable+" ("+City+" VARCHAR(255),"+rowid+" INTEGER PRIMARY KEY)";

//This is the cursor method i used for retrieve data from database in to SimpleCursor adapter

    public Cursor CitiesData(){
    SQLiteDatabase db=foodCourt_loginData.getReadableDatabase(); //calling database to get writable data
    Cursor citi=db.query(FoodCourt_LoginData.Citietable,new String[]{FoodCourt_LoginData.City,FoodCourt_LoginData.rowid},null,null,null,null,null);
   if(citi.moveToNext()){
     return citi;
   }else {
       Message.message(context, "there is nothing to show");
   }
    return citi;
}

//This is my homepage code that contains my simplecursor adapter

//Initializing database
    foodCourt_userLoginDatabase=new FoodCourt_UserLoginDatabase(rootView.getContext());

//This the code that is retrive the city database from SQLite Database into Simplecursor adapter

Cursor cursor=foodCourt_userLoginDatabase.CitiesData();
String[] cities = new String[]{FoodCourt_UserLoginDatabase.FoodCourt_LoginData.City};
  int[] to=new int[]{R.id.citispinner};
  mycursoradapter=new SimpleCursorAdapter(rootView.getContext().getApplicationContext(),android.R.layout.simple_spinner_item,cursor,cities,to,0);
   mycursoradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Citieslist.setAdapter(mycursoradapter);

//This is the error log i am getting and i dont know why it is stating that '_id' is not available but i created _id column with the name of rowid..

    01-13 15:44:02.083 23855-23855/com.example.dell.foodcourt E/SQLiteLog: (1) no such column: _id
01-13 15:44:02.098 1380-1422/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
01-13 15:44:02.107 23855-23855/com.example.dell.foodcourt D/AndroidRuntime: Shutting down VM
01-13 15:44:02.109 1380-1422/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
01-13 15:44:02.113 23855-23855/com.example.dell.foodcourt E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.dell.foodcourt, PID: 23855
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.foodcourt/com.example.dell.foodcourt.Userhome}: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT Citi, _id FROM CityTable
                                                                                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: _id (code 1): , while compiling: SELECT Citi, _id FROM CityTable
                                                                                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.queryWithFactory(SQLiteDatabase.java:1165)
                                                                                at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                                at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                                at com.example.dell.foodcourt.FoodCourt_UserLoginDatabase.CitiesData(FoodCourt_UserLoginDatabase.java:55)
                                                                                at com.example.dell.foodcourt.HomeFragment.onCreateView(HomeFragment.java:47)
                                                                                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)

 

Satish Rongala
  • 209
  • 6
  • 19
  • You've used `CREATE TABLE IF NOT EXISTS` which means that if there is already a table with a different structure, this statement won't replace it. – Titus Jan 13 '18 at 10:31
  • So you mean that I have to remove that if not exist statement?? – Satish Rongala Jan 13 '18 at 10:49
  • Just check if there is already a table which doesn't have the correct structure and if that is the case, delete it so the new table with the correct structure can be created. – Titus Jan 13 '18 at 10:50
  • Titus I tried that but it is still same...app is getting crashed after logged into the application – Satish Rongala Jan 13 '18 at 11:01

0 Answers0