2016-10-22 345 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