本文介绍了'_id'不存在,尽管ID列已更改为_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的 ID 重命名为 _id ,但仍获得列'_id'不存在 ...难道我错过了什么?

MyDatabaseelper.java

 公共类MyDatabaseHelper扩展SQLiteOpenHelper {
    公共静态最终诠释DATABASE_VERSION = 1;
    公共静态最后弦乐DATABASE_NAME =alm.db;
    公共静态最后弦乐TABLE_INFO =信息;
    公共静态最后弦乐TABLE_WORKFORCE =劳动力;
    公共静态最后弦乐TABLE_WORKDETAILS =WorkDetails;
    公共静态最后弦乐分包商=分包商;
    公共静态最后弦乐NumberOfPerson =NumberOfPerson;
    公共静态最后弦乐NUMBEROFHOURS =NUMBEROFHOURS;
    公共静态最后弦乐ID =_ ID;
    公共静态最后弦乐TimeIn_Info =timeIn_Info;
    公共静态最后弦乐TimeOut_Info =timeOut_Info;  公共无效的onCreate(SQLiteDatabase DB){
        db.execSQL(CREATE TABLE+ TABLE_INFO +(ID INTEGER PRIMARY KEY,Name文本,天气文本,日期DATETIME,状态文本,TimeIn_Info约会时,TimeOut_Info DATETIME));
        db.execSQL(CREATE TABLE+ TABLE_WORKFORCE +(ID INTEGER PRIMARY KEY,分包商TEXT,NumberOfPerson INTEGER,NUMBEROFHOURS TEXT,INTEGER TInfo_id,外键(TInfo_id)参考+ TABLE_INFO +(ID)));}

ListDisplay.java

 公共类ListDisplay扩展AppCompatActivity {
    InfoAPI sqlcon;
    私人SimpleCursorAdapter DataAdapter的;
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.listdisplay1);
        sqlcon =新InfoAPI(本);
        最后弦乐名1 = getIntent()getExtras()的getString(名称)。;
        BuildList(名称1);
    }    公共无效BuildList(字符串名称){
        最后弦乐名1 =名称;
        sqlcon.open();
        光标光标= sqlcon.readEntry(名称1);       的String [] =列新的String [] {
               MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut
       };        INT []为= INT新[] {
                R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
        };        //创建使用光标指向所希望的数据的转接器
        //以及布局信息
        DataAdapter的=新SimpleCursorAdapter(
                对此,R.layout.listdispaly,
                光标,
                列,
                至,
                0);        ListView控件的ListView =(ListView控件)findViewById(R.id.listView1);
        //指定适配器的ListView
        listView.setAdapter(DataAdapter的);
    }}

InfoAPI.java

 公共光标readEntry(字符串名称)
    {
        光标C = database.rawQuery(SELECT天气,日期,状态,TimeIn_Info,TimeOut_Info FROM+ MyDatabaseHelper.TABLE_INFO +其中name =?,新的String [] {将String.valueOf(名)},NULL);        如果(C!= NULL){
            c.moveToFirst();
        }
        返回℃;
    }
}

LogCat中错误

 产生的原因:java.lang.IllegalArgumentException异常:列'_id'不存在
            在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
            在android.widget.CursorAdapter.init(CursorAdapter.java:180)
            在android.widget.CursorAdapter<&初始化GT;(CursorAdapter.java:157)
            在android.widget.ResourceCursorAdapter<&初始化GT;(ResourceCursorAdapter.java:96)
            在android.widget.SimpleCursorAdapter<&初始化GT;(SimpleCursorAdapter.java:104)
            在com.example.project.project.ListDisplay.BuildList(ListDisplay.java:49)
            在com.example.project.project.ListDisplay.onCreate(ListDisplay.java:31)
            在android.app.Activity.performCreate(Activity.java:6237)
            在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
在android.app.ActivityThread.-wrap11(ActivityThread.java)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)


解决方案

在创建表的查询仍在使用ID(硬codeD字符串)

 公共无效的onCreate(SQLiteDatabase DB){
   db.execSQL(CREATE TABLE+ TABLE_INFO +(+ ID +INTEGER PRIMARY KEY,Name文本,天气文本,日期DATETIME,状态文本,TimeIn_Info约会时,TimeOut_Info DATETIME));
   db.execSQL(CREATE TABLE+ TABLE_WORKFORCE +(+ ID +INTEGER PRIMARY KEY,分包商TEXT,NumberOfPerson INTEGER,NUMBEROFHOURS TEXT,INTEGER TInfo_id,外键(TInfo_id)参考+ TABLE_INFO +(ID))) ;
}

你将不得不迫使你要么分贝完全地卸载从您的设备的应用程序,然后重新安装或递增版本的升级。

SimpleCursorAdapter 希望 _id 中的光标。它添加到查询,例如

 光标C = database.rawQuery(选择_id,天气,日期,状态,TimeIn_Info,TimeOut_Info FROM+ MyDatabaseHelper.TABLE_INFO +其中name =?,新的String [] {将String.valueOf(名)},NULL);

I have rename my ID to _id, but still get column '_id' does not exist ...Have I missed anything?

MyDatabaseelper.java

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="alm.db";
    public static final String TABLE_INFO="Information";
    public static final String TABLE_WORKFORCE="WorkForce";
    public static final  String TABLE_WORKDETAILS="WorkDetails";
    public static final String Subcontractors="Subcontractors";
    public static final String NumberOfPerson="NumberOfPerson";
    public static final String NumberOfHours="NumberOfHours";
    public static final String ID="_id";
    public static final String TimeIn_Info="timeIn_Info";
    public static final String TimeOut_Info="timeOut_Info";

  public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_INFO + "(ID INTEGER PRIMARY KEY ,Name TEXT,Weather TEXT, Date DATETIME, Status Text, TimeIn_Info DATE TIME, TimeOut_Info DATETIME)");
        db.execSQL("create table " + TABLE_WORKFORCE + "(ID INTEGER PRIMARY KEY  ,Subcontractors TEXT,NumberOfPerson INTEGER,NumberOfHours TEXT, TInfo_id INTEGER, FOREIGN KEY(TInfo_id) REFERENCES "+TABLE_INFO+"(ID))");

}

ListDisplay.java

public class ListDisplay extends AppCompatActivity {
    InfoAPI sqlcon;
    private SimpleCursorAdapter dataAdapter;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdisplay1);
        sqlcon = new InfoAPI(this);
        final String name1 = getIntent().getExtras().getString("name");
        BuildList(name1);
    }

    public void BuildList(String name) {
        final String name1 = name;
        sqlcon.open();
        Cursor cursor=sqlcon.readEntry(name1);

       String[] columns=new String[]{
               MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn,MyDatabaseHelper.TimeOut
       };

        int[] to=new int[]{
                R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.listdispaly,
                cursor,
                columns,
                to,
                0);

        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);






    }

}

InfoAPI.java

  public Cursor readEntry(String name)
    {
        Cursor c=database.rawQuery("SELECT  Weather, Date, Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE Name = ?", new String[]{String.valueOf(name)}, null);

        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
}

LogCat Error

  Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
            at android.widget.CursorAdapter.init(CursorAdapter.java:180)
            at android.widget.CursorAdapter.<init>(CursorAdapter.java:157)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:96)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.example.project.project.ListDisplay.BuildList(ListDisplay.java:49)
            at com.example.project.project.ListDisplay.onCreate(ListDisplay.java:31)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
解决方案

you create table queries are still using "ID" (hard-coded string)

public void onCreate(SQLiteDatabase db) {
   db.execSQL("create table " + TABLE_INFO + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Weather TEXT, Date DATETIME, Status Text, TimeIn_Info DATE TIME, TimeOut_Info DATETIME)");
   db.execSQL("create table " + TABLE_WORKFORCE + " ( " + ID + " INTEGER PRIMARY KEY  ,Subcontractors TEXT,NumberOfPerson INTEGER,NumberOfHours TEXT, TInfo_id INTEGER, FOREIGN KEY(TInfo_id) REFERENCES "+TABLE_INFO+"(ID))");
}

you will have to force an upgrade of your db either uninstalling completly the app from your device and reinstalling it again or incrementing VERSION.

The SimpleCursorAdapter wants the _id in the cursor. Add it to the query, E.g.

 Cursor c=database.rawQuery("SELECT _id,  Weather, Date, Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE Name = ?", new String[]{String.valueOf(name)}, null);

这篇关于'_id'不存在,尽管ID列已更改为_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:00