Android 内容提供程序-更新所有行和列
我有一个名为cv的ContentValues[]数组,它进入我的SQLite数据库。为了简单起见,我的数据库有这3列(每行、日期和湿度的ID自动递增)。数据库有3行。下面是一个添加到单行的数据示例Android 内容提供程序-更新所有行和列,android,sqlite,android-contentprovider,Android,Sqlite,Android Contentprovider,我有一个名为cv的ContentValues[]数组,它进入我的SQLite数据库。为了简单起见,我的数据库有这3列(每行、日期和湿度的ID自动递增)。数据库有3行。下面是一个添加到单行的数据示例 ContentValues cv = new ContentValues(); cv.put(WeatherContract.WeatherData.COLUMN_DAY_OF_WEEK, day); cv.put(WeatherContract.WeatherData
ContentValues cv = new ContentValues();
cv.put(WeatherContract.WeatherData.COLUMN_DAY_OF_WEEK, day);
cv.put(WeatherContract.WeatherData.COLUMN_HUMIDITY, humidity);
这将循环3次,在向数据库中进行大容量插入之前,每个ContentValue都会放入我的ContentValue数组中
我的工作是每五分钟从服务器上获取一次新数据。所以我需要这些新的数据来替换旧的数据,我在语法上遇到了问题。我的ContentValues[]jsonResults中包含了新数据,然后有点困惑。如何更新表中的所有行?是否需要循环使用ContentResolver更新方法:
for (int i =0; i<jsonResults.length;i++){
context.getContentResolver().update(weatherQueryUri,jsonResults[i],null,null);
}
谢谢大家! 嗯,我不确定我的解决方案是否有效,但你可以试试。 为了用新数据替换旧数据,我首先对ContentResolver对象执行delete()然后执行bulkInsert()。这样,您就不必循环通过jsonResults
if (jsonResults != null && jsonResults.length != 0) {
/* Get a ContentResolver object to help delete and insert data */
ContentResolver contentResolver = context.getContentResolver();
/* Delete old data */
contentResolver.delete(weatherQueryUri,null,null);
/* Insert our new data into contentResolver */
contentResolver.bulkInsert(weatherQueryUri, jsonResults);
}
我曾想过这样做,但bulkInsert会导致ID更改,我需要它们保持不变。
if (jsonResults != null && jsonResults.length != 0) {
/* Get a ContentResolver object to help delete and insert data */
ContentResolver contentResolver = context.getContentResolver();
/* Delete old data */
contentResolver.delete(weatherQueryUri,null,null);
/* Insert our new data into contentResolver */
contentResolver.bulkInsert(weatherQueryUri, jsonResults);
}