我已将ID
重命名为_id
,但仍然获得column '_id' does not exist
...我错过了什么吗?
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)
答案 0 :(得分:3)
您创建的表查询仍然使用“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);
答案 1 :(得分:1)
在将ID更改为_id之后,您需要卸载应用程序以重新安装。您还可以覆盖在SQLiteOpenHelper类下的辅助方法OnUpgrade方法,通过为数据库提供不同的版本号来升级数据库,然后删除旧的数据库版本。这可以确保您每次数据库发生更改时都不必重新安装应用程序。
您可以使用以下代码sinnipet。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS donotes");
//Create the data base with the New Data
onCreate(db);
}
像这样使用:
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));");
}