2013-08-27 90 views
0

我正在從數據庫中保存和提取數據,關鍵是從日曆中選擇日期。android數據庫:如何從數據庫中正確提取數據?

這是一種每天記錄的體重數據。如果我已將所需數據輸入到A日,然後繼續提取A日的數據,則一切正常。

我的問題:

如果我有一天不輸入任何東西,直接去提取了當天的數據,它會崩潰。我如何修改代碼,以便用戶可以安全地獲得Toast說「如果沒有DB中的記錄,用戶今天沒有輸入記錄,找不到記錄」?

代碼:

protected void onResume() 
{ 
    super.onResume(); 
    try 
    { 
     new LoadDataTask().execute(record_day_int); 
    } 
    catch (SQLiteException e) 
    { 
     System.err.println("Exception Message: " + e.getMessage()); 
    } 
} 

    private class LoadDataTask extends AsyncTask<Long, Object, Cursor> 
    { 
     DataBaseHelper databaseConnector = new DataBaseHelper(Calendar_view.this); 

     @Override 
     protected Cursor doInBackground(Long... params) 
     { 
     databaseConnector.open(); 
     return databaseConnector.getOneRecord(params[0]); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) 
     { 
     super.onPostExecute(result); 

     result.moveToFirst(); 

     int weight_kg01Index = result.getColumnIndex("weight_kg01"); 
      weight_data.setText(""+result.getInt(weight_kg01Index)); 

     result.close(); 
     databaseConnector.close(); 
     } 
    } 

logcat的:

08-28 01:06:16.790: W/dalvikvm(19928): threadid=1: thread exiting with uncaught exception (group=0x412282a0) 
08-28 01:06:16.790: E/AndroidRuntime(19928): FATAL EXCEPTION: main 
08-28 01:06:16.790: E/AndroidRuntime(19928): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.abc.abc.Calendar_view$LoadContactTask.onPostExecute(Calendar_view.java:233) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.abc.abc.Calendar_view$LoadContactTask.onPostExecute(Calendar_view.java:1) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask.finish(AsyncTask.java:631) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.Looper.loop(Looper.java:137) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.app.ActivityThread.main(ActivityThread.java:4898) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at java.lang.reflect.Method.invokeNative(Native Method) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at java.lang.reflect.Method.invoke(Method.java:511) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
08-28 01:06:16.790: E/AndroidRuntime(19928): at dalvik.system.NativeStart.main(Native Method) 

回答

0

if(result.moveToFirst()){ 
    int weight_kg01Index = result.getColumnIndex("weight_kg01"); 
     weight_data.setText(""+result.getInt(weight_kg01Index)); 
} 
result.close(); 

這樣,如果光標爲空,則它會忽略

+0

非常感謝!有用! – pearmak

+0

也值得檢查'Cursor'不爲null。例如'if(result!= null && result.moveToFirst()){...}' – Squonk

+0

@Squonk光標不應該爲空 – tyczj