2015-10-17 44 views
0

列'_id'不存在虽然我已将ID重命名为_id,但仍得到column '_id' does not exist ...我错过了任何内容吗?尽管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) 

回答

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

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

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

我仍然得到同样的错误 – John

+0

我编辑我的回答 – Blackbelt

+0

我创建了一个新的数据库,但它仍然是相同的 – John

1

您需要卸载应用程序以在将ID更改为_id后重新安装。您也可以重写OnUpgrade方法,该方法是SQLiteOpenHelper类下的helper方法,通过向数据库提供不同的版本号来升级数据库,然后删除旧数据库版本。这可确保您在每次数据库发生更改时都不必重新安装应用程序。

你可以使用下面的代码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));"); 

} 
+0

我已经创建了一个新的分贝,但它仍然是一样的。 – John

+0

我编辑了我的文章。使用更新代码片段。需要放置创建调用结束时的分号。 – Alok

+0

感谢您的帮助:) – John

相关问题