游标db.query() - SQLiteException:没有这样的列:_id

时间:2015-10-18 04:46:46

标签: java android sqlite cursor

我正在尝试构建一个使用带有多个关系表的SQLite的Android应用程序。但是一开始,我只想用一张桌来测试应用程序 我有DBadapter.java,每个其他表都有自己的适配器(tableAdapter)。我想测试的表只有具有InstructorAdapter的教练表(你会将其视为 EgitmenAdapter.java - Egitmen =讲师

我有一个包含getter& setter和constructors的模型类。( Egitmen.java

从主要活动中,点击按钮,名为 EgitmenActivity.java 活动将从我想要列出ListView中的教师ID,姓名和姓氏信息开始。

到目前为止,我在SO或其他网站上阅读了数百条建议。尝试了很多。查询之前的Open()数据库连接,版本增量,删除应用程序,将列名更改为SimpleCursorAdapter的Key_ID coz可能无法正常工作,一次又一次地检查CreateTable字符串......等等。我可能在尝试时做了一些错误,而不是当然。我不想重复任何问题。只是想让你知道,我尝试了很多东西,这就是为什么随着时间的推移,我的代码看起来对我来说更加陌生。无论如何,我处理了很多问题,但我坚持这个:

 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)

我不知道我是否遗漏了什么。长时间工作我正在处理bug,所以我可能会错过这么简单的事情。如果是这样,抱歉花时间。我在这里找到了许多关于no such column:_id问题的主题,并尝试了所有这些主题。有时候,我没有收到此错误,但那次我收到NullPointerException错误。

让我从DBadapter,EgitmenAdapter和EgitmenActivity java类中提供一些相关信息。

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

+如果您需要了解自定义egitmenitem_layout.xml:它只包含3个TextView,位于LinearLayout内的同一行。

+我可以将数据插入数据库并查看数据库文件中的记录。当我尝试从db中提取数据时,我确保db中有数据。

如果您需要更多信息,请告诉我。

提前感谢您的时间!

新错误代码:

Process: com.so.me.myapp, PID: 5055
    java.lang.IllegalArgumentException: column '_id' does not exist
            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
            at android.widget.CursorAdapter.init(CursorAdapter.java:172)
            at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.so.me.myapp.EgitmenActivity.EgitmenleriListele(EgitmenActivity.java:71)
            at com.so.me.myapp.EgitmenActivity$1.onClick(EgitmenActivity.java:50)

错误指向行:底部代码块为71:

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

1 个答案:

答案 0 :(得分:1)

你的logcat推送是什么

  

android.database.sqlite.SQLiteException:没有这样的列:_id(代码1):   ,同时编译:SELECT _id,Egitmen_AD,Egitmen_SOYAD FROM   tblEgitmen

使用Egitmen_ID代替_id

<强> public static final String KEY_ID = "Egitmen_ID";

Android. SQLite Exception: no such column _id

尝试这种方式,我希望它能帮到你。