我正在使用OpenWeatherMap API以JSON格式提取当前天气数据。
以下数据是一个例子:
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else {
return
}
// `input` is defined and initialized now ...
captureSession = AVCaptureSession()
captureSession?.addInput(input)
// ...
作为“虚拟条目”,可以在用户选择的任何时候删除,这三个城市插入onCreate()(虽然现在我明白为什么这可能是一个坏主意)
我的目的是让这三个虚拟记录更新onCreate()或onResume(),而不创建自己的新记录,然后将其添加到ListView。
添加新城市将拉出当前天气数据并将其添加到表格中,然后使用实时数据进行更新。
我的想法是在插入之前检查记录是否已经存在,而不是插入新条目,它会找到并设置温度和任何其他相关字段到新数据。
但我很无能,如何开始“检查”。这是我的插入方法:
{"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}
编辑:
SCHEMA:
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();
}
第二次编辑:
答案 0 :(得分: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 时checkIfRecordExists(上下文," weather_data"," ZIP"," DUMMY_ZIP_1234&# 34),
该查询转换为 SELECT 1 FROM weather_data WHERE ZIP =" DUMMY_ZIP_1234"
运行选择查询,将结果保存在游标中并检查游标是否包含任何记录。如果游标包含某些记录,则表示该记录已存在。
PS:如果邮政编码是唯一的,请使用邮政编码作为主键