2012-05-26 113 views
1

我想讓我的應用程序顯示我在數據庫中設置的表中的信息。從我收集的數據庫設置正確,代碼是正確的,但我不知道是什麼導致了錯誤。我會發布所有的代碼,看看這裏有沒有人可以幫助我解決問題。無法在ListView中顯示數據庫

Progress.class:

public class WorkoutProgress extends ListActivity { 
    private DataBaseHelper datasource; 
    TextView goal; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.progress); 
     goal = (TextView)findViewById(R.id.goal); 
     datasource = new DataBaseHelper(this); 
     datasource.open(); 
     fillData(); 
     datasource.close(); 
    } 
    private void fillData() { 
    Cursor c = datasource.getAllActs(); 
    startManagingCursor(c); 
    String[] from = new String[] {DataBaseHelper.KEY_DATE, 
    DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs }; 
    int[] to = { R.id.code, R.id.Days, R.id.BMI }; 
    SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to); 
    setListAdapter(notes);    
    } 
} 

數據庫助手:

public class DataBaseHelper 
{ 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_GOAL = "goal"; 
    public static final String KEY_Current = "cweight"; 
    public static final String KEY_Target = "nweight"; 

    public static final String KEY_ROWID2 = "_id"; 

    public static final String KEY_DATE = "date"; 
    public static final String KEY_STEPS = "steps"; 
    public static final String KEY_CALs = "calories"; 
    public static final String KEY_CALID = "_id"; 
    public static final String KEY_TOTALCALS = "totalcals";  
    private static final String TAG = "DBAdapter"; 
    private static final String DATABASE_NAME = "workout"; 
    private static final String DATABASE_TABLE = "goals"; 
    private static final String DATATABLE = "acts"; 
    private static final String CALTABLE = "cals"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_CREATE = 
    "create table goals (_id integer primary key autoincrement, " 
    + "goal text not null, cweight number not null, " 
    + "nweight number not null);"; 
    private static final String DATABASE_CREATE2 = 
     "create table acts (_id integer primary key autoincrement, " 
       + "date text not null, steps text not null, " 
       + "calories number not null);" 
    private final Context context; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 
    public DataBaseHelper(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
      db.execSQL(DATABASE_CREATE2); 
     } 
     @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 titles"); 
      onCreate(db); 
     } 
    }  
    public DataBaseHelper open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 
    public void close() 
    { 
     DBHelper.close(); 
    } 
    public long insertTitle(String goal, int current, int target) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_GOAL, goal); 
     initialValues.put(KEY_Current, current); 
     initialValues.put(KEY_Target, target); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public long insertActivity(String date, String steps, double Calories) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_DATE, date); 
     initialValues.put(KEY_STEPS, steps); 
     initialValues.put(KEY_CALs, Calories); 
     return db.insert(DATATABLE, null, initialValues); 
    } 
    public boolean deleteTitle(long rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + 
      "=" + rowId, null) > 0; 
    } 
    public void deleteFirst() 
    { 
     Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 
     if(cursor.moveToFirst()) { 
     long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 
     db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null); 
    } 
} 

public boolean deleteAct(long rowId) 
{ 
    return db.delete(DATATABLE, KEY_ROWID + 
      "=" + rowId, null) > 0; 
} 
//---retrieves all the titles--- 
public Cursor getAllGoals() 
{ 
    return db.query(DATABASE_TABLE, new String[] { 
      KEY_ROWID, 
      KEY_GOAL, 
      KEY_Current, 
      KEY_Target}, 
      null, 
      null, 
      null, 
      null, 
      null); 
} 
public Cursor getAllActs() 
{ 
    return db.query(DATATABLE, new String[] { 

      KEY_ROWID2, 

      KEY_DATE, 
      KEY_STEPS, 
      KEY_CALs}, 
      null, 
      null, 
      null, 
      null, 
      null); 
} 

//---retrieves a particular title--- 
public Cursor getGoal 
(long rowId) throws SQLException 
{ 
    Cursor mCursor = 
      db.query(true, DATABASE_TABLE, new String[] { 
        KEY_ROWID, 
        KEY_GOAL, 
        KEY_Current, 
        KEY_Target 
        }, 
        KEY_ROWID + "=" + rowId, 
        null, 
        null, 
        null, 
        null, 
        null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 
public Cursor getAct(long rowId) throws SQLException 
{ 
    Cursor mCursor = 
      db.query(true, DATATABLE, new String[] { 
        KEY_ROWID2, 
        KEY_DATE, 
        KEY_STEPS, 
        KEY_CALs 
        }, 
        KEY_ROWID2 + "=" + rowId, 
        null, 
        null, 
        null, 
        null, 
        null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 
public boolean updateAct(long rowId, String date, 
     String steps, String cals) 
     { 
      ContentValues args = new ContentValues(); 
      args.put(KEY_DATE, date); 
      args.put(KEY_STEPS, steps); 
      args.put(KEY_CALs, cals); 
      return db.update(DATATABLE, args, 
          KEY_ROWID2 + "=" + rowId, null) > 0; 
     } 
/*public boolean updateCals(long rowId, double cals) 
     { 
      ContentValues args = new ContentValues(); 
      args.put(KEY_TOTALCALS, cals); 
      return db.update(CALTABLE, args, 
          KEY_CALID + "=" + rowId, null) > 0; 
     }*/ 

//---updates a title--- 
public boolean updateTitle(long rowId, String isbn, 
String title, String publisher) 
{ 
    ContentValues args = new ContentValues(); 
    args.put(KEY_GOAL, isbn); 
    args.put(KEY_Current, title); 
    args.put(KEY_Target, publisher); 
    return db.update(DATABASE_TABLE, args, 
        KEY_ROWID + "=" + rowId, null) > 0; 
} 
} 

logcat的:

05-26 14:42:03.895: E/AndroidRuntime(345): FATAL EXCEPTION: main 
05-26 14:42:03.895: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutProgress}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, date, steps, calories FROM acts 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.os.Looper.loop(Looper.java:123) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-26 14:42:03.895: E/AndroidRuntime(345): at java.lang.reflect.Method.invokeNative(Native Method) 
05-26 14:42:03.895: E/AndroidRuntime(345): at java.lang.reflect.Method.invoke(Method.java:521) 
05-26 14:42:03.895: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-26 14:42:03.895: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-26 14:42:03.895: E/AndroidRuntime(345): at dalvik.system.NativeStart.main(Native Method) 
05-26 14:42:03.895: E/AndroidRuntime(345): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, date, steps, calories FROM acts 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264) 
05-26 14:42:03.895: E/AndroidRuntime(345): at com.b00348312.workout.DataBaseHelper.getAllActs(DataBaseHelper.java:168) 
05-26 14:42:03.895: E/AndroidRuntime(345): at com.b00348312.workout.WorkoutProgress.fillData(WorkoutProgress.java:30) 
05-26 14:42:03.895: E/AndroidRuntime(345): at com.b00348312.workout.WorkoutProgress.onCreate(WorkoutProgress.java:25) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-26 14:42:03.895: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
05-26 14:42:03.895: E/AndroidRuntime(345): ... 11 more 

找錢g在logcat它似乎告訴我我錯過了一列"_id"但我修改了數據庫以包含此列,您可以在上面的數據庫幫助程序代碼中看到該列。有什麼我錯過了嗎?

回答

1

您可能已修改數據庫以包含所需的列,但在數據庫中實際不會實施此更改,除非再次調用onCreate方法,或者您在onUpgrade方法中實施更改並修改數據庫的版本。

嘗試卸載並再次在模擬器/手機上安裝應用程序,以便再次調用SQLiteOpenHelper實現的onCreate方法。

+0

這似乎不工作作爲更新數據庫,重新安裝它給出相同的logcat錯誤。我不僅在自己的手機上運行了此操作,還在其他手機和模擬器上運行了這些操作,並在新數據庫實施後給出了相同的結果 –

+0

@DarrenMurtagh在重新安裝應用程序之前是否刪除了所有數據(使用'Clear Data')? – Luksprog

+1

我試圖做的一點是遊標結果集必須包含_id。你是否已經通過並驗證了這一點? – Simon

1

隨着活動不會啓動錯誤,根本原因是進一步在堆棧中。在你的情況下,在這裏:

05-26 14:42:03.895: E/AndroidRuntime(345): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, date, steps, calories FROM acts 

這似乎不言自明?請參閱here

關鍵在於您的遊標必須在結果集中包含名爲_id的列,可以來自實列或別名。

+0

但它包含_id。它的主鍵或表,它應該從 –

+0

拉動信息如果更改主鍵不是我應該有什麼我應該有什麼與列_id –

相關問題