samedi 17 octobre 2015

Cursor db.query() - SQLiteException: no such column: _id

I am trying to build an Android application which uses SQLite with multiple relational tables. But at the start, I just want to test the app by using one table only.
I have DBadapter.java and every other table has their own adapters(tableAdapter). The table i wanna test only is Instructor table which has InstructorAdapter (You will see it as EgitmenAdapter.java - Egitmen=Instructor)

I have model class for Instructor which contains getter&setters and constructors.(Egitmen.java)

From mainactivity, by tapping a button, an activity called EgitmenActivity.java starts where I want to list Instructor ID,name and surname informations in a ListView.

Up until now, I read maybe hundreds of advices in SO or in other sites. Tried many of them. Open() database connection before query, version increment, delete app, change column name to Key_ID coz of SimpleCursorAdapter may not work, checking the CreateTable Strings again and again..etc etc.. I might have done some wrong while trying them,not sure. I don't want to repeat any question. Just want you to know that, I tried many things and that's why my code looks more stranger to me as time goes by.. Anyway, I handled many problems but am stuck at this one:

 10-18 06:36:25.888    6313-6313/? E/SQLiteLog﹕ (1) no such column: _id
10-18 06:36:25.905    6313-6313/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, Egitmen_AD, Egitmen_SOYAD FROM tblEgitmen
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
            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:1314)
            at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
            at com.so.me.myapp.adapters.EgitmenAdapter.egitmenAdSoyadGetir(EgitmenAdapter.java:114)
            at com.so.me.myapp.EgitmenActivity.EgitmenleriListele(EgitmenActivity.java:69)
            at com.so.me.myapp.EgitmenActivity$1.onClick(EgitmenActivity.java:50)
            at android.view.View.performClick(View.java:4211)
            at android.view.View$PerformClick.run(View.java:17446)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5336)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

I don't know if I am missing something. For long hours I am working on bugs so i might be missing something so easy. If so, sorry for taking your time. I found many topics opened here about no such column:_idproblems and tried all of them. There were times, I didn't get this error but that time I got a NullPointerException error.

Let me give you some pertinent information from my DBadapter, EgitmenAdapter and EgitmenActivity java classes.

DBAdapter.java

/*.....skipped......*/
/************ TABLE CREATE ***********/
    private static final String TABLE_EGT_CREATE = "CREATE TABLE IF NOT EXISTS "+TABLE_EGITMEN+
            "(Egitmen_ID INTEGER PRIMARY KEY AUTOINCREMENT ,"+
            "Egitmen_AD TEXT NOT NULL ,"+
            "Egitmen_SOYAD TEXT NOT NULL ,"+
            "Egitmen_PAROLA TEXT NOT NULL,"+
            "Egitmen_MESLEK TEXT,"+
            "Egitmen_OZGECMIS TEXT,"+
            "Egitmen_TEL TEXT,"+
            "Egitmen_MAIL TEXT,"+
            "Egitmen_LINKEDIN_URL TEXT,"+
            "Egitmen_FACEBOOK_URL TEXT,"+
            "Egitmen_TWITTER_URL TEXT,"+
            "Egitmen_WEBSITE_URL TEXT,"+
            "Egitmen_BANLANDI_MI INTEGER DEFAULT 0)";   

/*.....skipped......*/

EgitmenAdapter.java

        public class EgitmenAdapter extends DBAdapter {

            /************ COLUMNS ************/
            public static final String KEY_ID = "_id";
            public static final String COL_EGT_AD = "Egitmen_AD";
            public static final String COL_EGT_SOYAD = "Egitmen_SOYAD";
            public static final String COL_EGT_PAROLA = "Egitmen_PAROLA";
            public static final String COL_EGT_MESLEK = "Egitmen_MESLEK";
            public static final String COL_EGT_OZGECMIS = "Egitmen_OZGECMIS";
            public static final String COL_EGT_TEL = "Egitmen_TEL";
            public static final String COL_EGT_MAIL = "Egitmen_MAIL";
            public static final String COL_EGT_LINKEDIN = "Egitmen_LINKEDIN_URL";
            public static final String COL_EGT_FACEBOOK = "Egitmen_FACEBOOK_URL";
            public static final String COL_EGT_TWITTER = "Egitmen_TWITTER_URL";
            public static final String COL_EGT_WEBSITE = "Egitmen_WEBSITE_URL";
            public static final String COL_EGT_BANLANDIMI = "Egitmen_BANLANDI_MI";


            /************ CONSTRUCTOR ************/
            public EgitmenAdapter(Context ctx)
            {
                super(ctx);
            }

        /*.....skipped......*/        
            /************ GETS ID,NAME AND SURNAMES  ************/ 
            public Cursor egitmenAdSoyadGetir()
            {
                db = dbHelper.getWritableDatabase();
               String [] egitmenKolonlar = new String [] {KEY_ID,COL_EGT_AD,COL_EGT_SOYAD};
 /*Line:114*/  Cursor c = db.query(TABLE_EGITMEN,egitmenKolonlar,null,null,null,null,null);
                if(c != null){
                    c.moveToFirst();
                }
                else
                {
                    Log.e("HATA CURSOR : "," NULL DÖNDÜ !");
                }
                return c;
            }

EgitmenActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_egitmen);
        btnEgitmenListele = (Button) findViewById(R.id.buttonEgitmenListele);
        egtListe= (ListView) findViewById(R.id.listViewEgitmen);
        egtListe.setBackgroundColor(Color.LTGRAY);(this,android.R.layout.simple_selectable_list_item);


        btnEgitmenListele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbOpen();
/*Line:50*/     EgitmenleriListele();
            }
        });
      }

/*.....skipped.....*/

  public void EgitmenleriListele()
    {
        Cursor cursor = egitmenAdapter.egitmenAdSoyadGetir();
        String [] kolonlar = new String[] {egitmenAdapter.KEY_ID,egitmenAdapter.COL_EGT_AD,egitmenAdapter.COL_EGT_SOYAD};
/*Line:69*/  int [] tutucuText = new int[] {R.id.textViewEgitmenItemID,R.id.textViewEgitmenItemAD,R.id.textViewEgitmenItemSOYAD};
        SimpleCursorAdapter sCursorAdapter;
        sCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.egitmenitem_layout, cursor, kolonlar,tutucuText,0);
        egtListe.setAdapter(sCursorAdapter);
}

+If you need to know about custom egitmenitem_layout.xml : It just contains 3 TextViews at the same row, inside a LinearLayout.

+I can insert data to database and see the records in the database file. When i try to pull data from db , i make sure there is data in db.

If you need further information, please let me know.

Thank you in advance for your time!

Aucun commentaire:

Enregistrer un commentaire