gpt4 book ai didi

java - 尽管 ID 已更改为 _id,但列 '_id' 不存在

转载 作者:行者123 更新时间:2023-12-01 18:10:45 24 4
gpt4 key购买 nike

我已将我的 ID 重命名为 _id,但仍然得到列“_id”不存在 ...我错过了什么吗?

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 错误

  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)

最佳答案

您创建的表查询仍在使用“ID”(硬编码字符串)

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

您必须强制升级数据库,要么从设备上完全卸载应用程序,然后重新安装,要么增加版本。

SimpleCursorAdapter 想要光标中的 _id。将其添加到查询中,例如

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

关于java - 尽管 ID 已更改为 _id,但列 '_id' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com