使用 DatePicker 日期从数据库获取数据时出现问题
嘿,我这里遇到问题了。
我试图根据传递给该方法的日期从数据库中检索日期,并且它返回该值。
问题是,当我尝试传递接收日期的变量时,从数据库中选择数据的方法不会返回任何内容。 (我在 LogCat 上打印日期变量,没有问题,日期值是正确的),但是如果我传递像这样的字符串值(“1/01/1111”),它会正确返回。
这是获取值并设置文本的活动的方法。
public void setBasicContent() {
date = (mMonth + 1) + "/" + mDay + "/" + mYear + " ";
hpdData = this.hpd.selectDuration(date);
mDateDisplay.setText(hpdData);
}
这里是 selectDuration() 方法,它根据日期参数从数据库中选择数据。
啊,当我在活动中传递变量日期时,代码没有到达 if(cursor.moveToFirst())
范围。但我不知道为什么,因为变量值完全正确,就像普通字符串一样。
public String selectDuration(String date) {
String duration = "";
Integer value = 0;
String returnment = "";
Log.i(TAG, "date to select: " + date);
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "duration" },
"date = ?", new String[] { date }, null, null, null);
if (cursor.moveToFirst()) {
do {
Log.i("SELECTDURATION", "inside cursor.moveToFirst()");
duration = cursor.getString(0);
value += Integer.parseInt(duration);
} while (cursor.moveToNext());
returnment = Integer.toString(value);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
Log.i(TAG, "valor do returnment: " + returnment);
return returnment;
}
Hey I got a problem here.
I'm trying to retrieve the date from the Database based on Date passed to the method, and it returns the value.
The problem is that, when I try to pass the variable that receives the date, the method that select the data from the DB, returns nothing. (and I print the date variable on the LogCat and it's ok, the date value is correct), but if I pass a String value like this ("1/01/1111") it returns correctly.
here is the method on the activity that get the value and set the text.
public void setBasicContent() {
date = (mMonth + 1) + "/" + mDay + "/" + mYear + " ";
hpdData = this.hpd.selectDuration(date);
mDateDisplay.setText(hpdData);
}
And here is the selectDuration() method that select the data from the DB based on the date parameter.
Ah, when I pass the variable date in the activity, the code doesn't reach the if(cursor.moveToFirst())
scope. But I don't know why, because the variable value is completely correctly exactly like a normal string.
public String selectDuration(String date) {
String duration = "";
Integer value = 0;
String returnment = "";
Log.i(TAG, "date to select: " + date);
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "duration" },
"date = ?", new String[] { date }, null, null, null);
if (cursor.moveToFirst()) {
do {
Log.i("SELECTDURATION", "inside cursor.moveToFirst()");
duration = cursor.getString(0);
value += Integer.parseInt(duration);
} while (cursor.moveToNext());
returnment = Integer.toString(value);
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
Log.i(TAG, "valor do returnment: " + returnment);
return returnment;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了错误。它位于
setBasicContent()
方法上。这是旧方法:
}
这是修改后的新方法:
}
问题出在这一行:
如果您看到,它将一个空字符连接到日期,因此在 String 中将传递带有空字符的日期字符串,这对于字符串来说是有区别的。所以它一定是这样的:
I found the error. It is on the
setBasicContent()
method.Here is the old method:
}
and here is the new method modified:
}
The problem is on this line:
If you see, it concatenates an empty character to the date, so in the String will pass the date string with an empty character, which for a String makes the difference. So it must be like this:
如果您的数据库引用实例化正常,我在您的代码中看不到任何明显不正确的内容。如果可能,请将日期作为整数存储在数据库中,并存储 date.toTime() 中的毫秒值。然后,从 new Date(milliseconds) 实例化日期对象会更容易,然后您可以根据区域设置格式化输出。
I can't see anything obviously incorrect in your code, provided your db reference is instantiated ok. If possible, store your dates as integers in the datbase, storing the milliseconds value from date.toTime(). It's then much easier to instantiate a date object from new Date(milliseconds) and you can then format the output according to the locale.
我已在我的博客上写下了执行此操作的方法。我转换为 Calendar 对象并使用 getTimeInMillis()。这样您只需在数据库中存储一个“长”值。
在 SQLite 数据库中存储 DatePicker 和 TimePicker
I have written my method of doing this on my blog. I convert to a Calendar object and use getTimeInMillis(). That way you only have to store a 'long' value in the database.
Storing a DatePicker and TimePicker in SQLite Database