2016-10-22 49 views
0

我使用OpenWeatherMap API以JSON格式提取當前天氣數據。在Android SQLITE數據庫中存儲新城市天氣數據並更新現有城市天氣數據

下面的數據是一個例子:

{"coord":{"lon":-83.05,"lat":42.33},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":48.1,"pressure":1004.19,"humidity":100,"temp_min":48.1,"temp_max":48.1,"sea_level":1028.21,"grnd_level":1004.19},"wind":{"speed":15.23,"deg":315.508},"clouds":{"all":0},"dt":1477154497,"sys":{"message":0.1743,"country":"US","sunrise":1477137281,"sunset":1477175856},"id":4990729,"name":"Detroit","cod":200} 
{"coord":{"lon":-87.65,"lat":41.85},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":51.38,"pressure":1009.38,"humidity":100,"temp_min":51.38,"temp_max":51.38,"sea_level":1031.85,"grnd_level":1009.38},"wind":{"speed":8.08,"deg":227.508},"clouds":{"all":48},"dt":1477154834,"sys":{"message":0.1714,"country":"US","sunrise":1477138345,"sunset":1477177000},"id":4887398,"name":"Chicago","cod":200} 
{"coord":{"lon":-74.01,"lat":40.71},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":48.14,"pressure":1008,"humidity":100,"temp_min":48.14,"temp_max":48.14,"sea_level":1011.38,"grnd_level":1008},"wind":{"speed":15.46,"deg":296.508},"rain":{"3h":2.2},"clouds":{"all":80},"dt":1477154848,"sys":{"message":0.1842,"country":"US","sunrise":1477134973,"sunset":1477173827},"id":5128581,"name":"New York","cod":200} 

作爲可以在用戶選擇的任何時間刪除「虛擬條目」,這三個城市被插入的onCreate()(雖然現在我明白爲什麼能是一個壞主意)

我的意圖是讓這三個虛擬記錄更新onCreate()或onResume()而不創建自己的新記錄,然後將其添加到ListView。

添加新城市將拉取當前天氣數據並將其添加到表格中,隨後使用實時數據進行更新。

我的想法是在插入之前檢查記錄是否已經存在,而不是插入新條目,它會查找並設置新數據的溫度和任何其他相關字段。

但我很笨,以及如何開始「檢查」。這裏是我的插入方法:

public void insertRow(CustomListData weather){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues insertValues = new ContentValues(); 
     insertValues.put("TEMP", weather.getTemperature()); 
     insertValues.put("CITYNAME", weather.getCity()); 
     insertValues.put("ZIP", weather.getZip()); 
     db.insert("weather_data", null, insertValues); 
     db.close(); 
} 

編輯:

架構:

CREATE TABLE weather_data(_id INTEGER PRIMARY KEY AUTOINCREMENT, TEMP VARCHAR(5), CITYNAME VARCHAR(255), ZIP VARCHAR(6)); 

第二個編輯:

WeatherDataStorage -- helper class

+0

請張貼架構。然後,我將能夠基於此建議您適當的解決方案。 – oathkeeper

+0

編輯已完成。 – Joseph

+0

正如你可以從這個教程http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/看到的,你需要創建一個sqlite助手,它可以執行諸如讀,插入,刪除,更新等數據庫上。當在教程中更新或刪除記錄時,它使用id作爲字段來標識該行。您需要將其更改爲名稱,以便使用城市名稱。 – Tasos

回答

0

如果需要檢查,如果記錄不存在在表格中,最快的方法是檢查這種方式(假設郵政編碼是所有記錄)獨特:

 public static boolean checkIfRecordExists(Context context, String tableName, 
           String tableAttribute, String attributeValue) { 

      Cursor cursor = context.getContentResolver().query(tableName, 
      new String[]{"1"}, 
      tableAttribute + "=?, 
      new String[]{attributeValue}, 
      null); 

      boolean recordExists = (cursor !=null && cursor.getCount() > 0); 
      cursor.close(); 

      return recordExists; 
    } 

所以,你可以調用此方法來檢查,如果你的「weather_data」表中存在的記錄。

public void insertRow(CustomListData weather){ 
SQLiteDatabase db = this.getWritableDatabase(); 

//(Context context, String tableName, String tableAttribute, String attributeValue) 

    boolean recordExists = checkIfRecordExists(context, "weather_data", "ZIP", weather.getZip()); 

    if(!recordExists) { 
     ContentValues insertValues = new ContentValues(); 
     insertValues.put("TEMP", weather.getTemperature()); 
     insertValues.put("CITYNAME", weather.getCity()); 
     insertValues.put("ZIP", weather.getZip()); 
     db.insert("weather_data", null, insertValues); 
     db.close(); 
    } 
} 

請儘量嚴格按照規範操作。您正試圖檢查表中的記錄是否與郵政編碼相關。

因此,當調用實用方法checkIfRecordExists與這些PARAMS checkIfRecordExists(上下文中, 「weather_data」, 「郵政編碼」, 「DUMMY_ZIP_1234」),

查詢轉換爲選擇1 FROM weather_data WHERE ZIP =「DUMMY_ZIP_1234」

您運行select查詢,將結果保存在遊標中,並檢查遊標是否保存任何記錄。如果遊標保存了一些記錄,則表示記錄已經存在。

PS:如果郵政編碼是唯一的,請使用郵政編碼作爲你的「weather_data」表的主鍵

+0

我將通過什麼上下文checkIfRecordExist() – Joseph

+0

在代碼中嘗試一次該程序。所有活動都擴展了Context。我只是提出了一個通用的實用方法。如果您在活動中使用此代碼,請調用以下代碼:checkIfRecordExists(this,「weather_data」,「ZIP」,「DUMMY_ZIP_1234」)。這是指活動。如果你正在使用一個片段,然後使用這個checkIfRecordExists(getActivity(),「weather_data」,「ZIP」,「DUMMY_ZIP_1234」), – oathkeeper

+0

如果這個答案對你有幫助,請提出答案並標記爲正確。 :)我們花了很多時間來澄清疑惑併發布我們的答案。 – oathkeeper