2014-12-28 41 views
0

我正在編寫一個應用程序,它將從遠程服務器上的xml文件中讀取數據,然後將其放入手機/選項卡上的SQLite數據庫中。我知道它正在成功地從xml讀取數據並將其放入SQLite .db文件中。但是,問題是我的應用程序的其餘部分的行爲就好像SQLite .db文件是空的。它將不會訪問數據,直到我退出應用程序並再次運行它。我究竟做錯了什麼?編寫和讀取SQLite中的SQLite數據庫時遇到困難

以下是我得到的數據:

// Get Instrument data from XML 
     while (eventType != XmlResourceParser.END_DOCUMENT) { 
      if (eventType == XmlResourceParser.START_TAG) { 
       // Get the name of the tag (eg data or datum) 
       String strName = instruments.getName(); 
       if (strName.equals("datum")) { 
        bFoundInstruments = true; 
        String datumDate = instruments.getAttributeValue(null, "date"); 
        Float datumWtioil = Float.parseFloat(instruments.getAttributeValue(null, "wtioil")); 
        Float datumHh = Float.parseFloat(instruments.getAttributeValue(null, "hh")); 
        Float datumPlat = Float.parseFloat(instruments.getAttributeValue(null, "plat")); 


        publishProgress(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat)); 

        // add data to SQLite database 
        datasource.createInstrument(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat)); 


       } 
      } 

現在,這裏就是我把它放到SQLite的.db文件使用createInstrument()方法:

public Instrument createInstrument(String instrumentDate, String instrumentWtioil, String instrumentHh, 
      String instrumentPlat) { 
    ContentValues values = new ContentValues(); 
    //Log.d("instrumentalityWtioil",instrumentWtioil); 
    values.put(ForecastSQLiteHelper.COLUMN_DATE, instrumentDate); 
    values.put(ForecastSQLiteHelper.COLUMN_DATA1, instrumentWtioil); 
    values.put(ForecastSQLiteHelper.COLUMN_DATA2, instrumentHh); 
    values.put(ForecastSQLiteHelper.COLUMN_DATA3, instrumentPlat); 
    long insertId = database.insert(ForecastSQLiteHelper.TABLE_DATA, null, 
     values); 
    Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA, 
     allColumns, ForecastSQLiteHelper.COLUMN_ID + " = " + insertId, null, 
     null, null, null); 
    cursor.moveToFirst(); 
    Instrument newInstrument = cursorToInstrument(cursor); 
    cursor.close(); 
    return newInstrument; 
    } 

爲什麼沒有數據立即可用於應用程序?爲什麼我必須關閉應用程序並在可見之前重新啓動它?

任何意見將不勝感激。

+0

從哪裏調用'createInstrument',你的意思是「它是可見的」(你的展示視圖是什麼)? – staaar

回答

3

首先刪除從該函數獲取數據庫值的查詢,因爲您嘗試將一個儀器插入到數據庫然後再讀回。執行此操作:
a)從xml文件中讀取數據並將它們添加到數據庫中,並b)創建一個函數,用於查詢並返回數據庫中的所有儀器。

public List<Instrument> getAllInstruments() { 
    // create a list 
    List<Instrument> list = new ArrayList< Instrument>(); 

    Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA, 
    allColumns, null, null, null, null, null); 

    // check if cursor has columns 
    if (cursor.getColumnCount() > 0) { 
     // read all cursor values 
     while (cursor.moveToNext()) { 
      Instrument newInstrument = new Instrument(); 
      // now you have to get the values from the cursor and add them to your object 
      // like this 
      String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME)); 
      // COLUMN_NAME is the name of the column in your database that you want the value of 

      // now add the values to the Instrument object 
     newInstrument.setName(instrumentName); 

     // now add the object to the list 
     list.add(newInstrument); 
     } 
    } 

    cursor.close(); 
    return list; //finally return the list 
} 
+0

感謝您的幫助 –

+0

您的歡迎。任何時候 –