[英]Android sqlite store data from List<LocalDate>
我正在嘗試將每個數據存儲為數據庫中的每一行。 我設法存儲了數據,但是所有數據都被存儲為一行。 順便說一句,我正在使用import org.joda.time.LocalDate
我得到的結果是:
|tbl_id | days
--------------------------------------------------------------------------------------------------------------------------------
|1 | 2016-9-10, 2016-9-11, 2016-9-12 2016-9-13, 2016-9-14, 2016-9-15, 2016-9-16, 2016-9-17, 2016-9-18, 2016-9-19, 2016-9-20
我之后是什么
|tbl_id | days
-------------------
|1 | 2016-9-10
|2 | 2016-9-11
|3 | 2016-9-12
這是我到目前為止所擁有的
方法天
String from = "2016-9-10";
String to = "2016-9-20";
SimpleDateFormat df = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
try {
String reformatDateFrom = myFormat.format(df.parse(from));
String reformatDateTo = myFormat.format(df.parse(to));
LocalDate start = LocalDate.parse(reformatDateFrom);
LocalDate end = LocalDate.parse(reformatDateTo);
List<LocalDate> days= new ArrayList<>();
while (!start.isAfter(end)) {
days.add(start);
start = start.plusDays(1);
}
Toast.makeText(this, "This" + days,
Toast.LENGTH_LONG).show();
databaseHandler.insertDays(days);
} catch (ParseException pe) {
pe.printStackTrace();
}
方法插入
public void insertDays(List<LocalDate> days) {
int size = days.size();
sqLiteDatabase = this.getWritableDatabase();
try {
for(int i = 0; i < size; i++) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_DAYS, String.valueOf(days.get(i)));
sqLiteDatabase.insert(TABLE_DAYS, null, contentValues);
}
sqLiteDatabase.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
sqLiteDatabase.close();
}
}
更改
databaseHandler.insertDays(String.valueOf(days));
至
databaseHandler.insertDays(days);
第一個版本將您的列表轉換為String
表示形式,並調用與此處顯示的方法不同的insertDays()
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.