我想要实现的基本想法是在SQlite数据库中比较当前Android系统日期和现有日期。Android - SQLite比较数据库中的日期到日期
的想法是1)如果数据库不包含包含今天的日期条目,则允许新进入
2)如果数据库已经包含今天的日期的条目,然后撤销入境和输出错误消息。
我将对DBAdapter目前拥有执行以下操作的方法:
public Cursor getDate(String date) {
String where = KEY_DATE + "=" + date;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c !=null) {
c.moveToFirst();
}
return c;
}
和我使用作出比较的方法是:
public void addRecord (View view){
db.open();
Cursor cursor = db.getDate(dateString);
if (cursor.moveToFirst()){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Date");
builder.setMessage("Entry for today exists, would you like to update this instead?"); builder.setPositiveButton("OK", null);
@SuppressWarnings("unused")
AlertDialog dialog = builder.show();
db.close();
}
else{
Do other work..
两个日期的用于比较的格式使用此代码:
long longDate = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(longDate);
其中我只是简单地调用'dateString'
我的问题是它不执行正确的检查并完全跳过我的else语句,即使我知道数据库中存在一个“日期”。我的SQL语句正确吗?
工作就像一个魅力!非常感谢! – user3197786