2011-02-04 66 views
0

嘿,我在这里遇到了问题。使用DatePicker date从数据库获取数据的问题

我想从数据库检索日期传递给该方法的日期,并且它返回值。

问题是,当我尝试传递接收日期的变量时,从数据库中选择数据的方法不会返回任何内容。 (我打印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; 
} 
+0

嗯,很难说这是怎么回事这里,因为我不知道发生了什么你的查询字符串的()内。也许尝试手动构建一个SQL查询并将其传递到rawQuery(),而不是?然后您可以查看正在执行的确切语句。 – 2011-02-04 19:08:19

回答

1

我发现了错误。这是在setBasicContent()方法。

这里是老方法:

public void setBasicContent() { 

date = (mMonth + 1) + "/" + mDay + "/" + mYear + " "; 
hpdData = this.hpd.selectDuration(date); 
mDateDisplay.setText(hpdData); 

}

,并在这里被修改的新方法:

public void setBasicContent() { 

date = (mMonth + 1) + "/" + mDay + "/" + mYear; 
hpdData = this.hpd.selectDuration(date); 
mDateDisplay.setText(hpdData); 

}

的问题是在这条线:

date = (mMonth + 1) + "/" + mDay + "/" + mYear + " "; 

如果你看到它将一个空字符连接到日期,那么在字符串中将会传递一个空字符的日期字符串,这对于一个字符串来说是不同的。所以它一定是这样的:

date = (mMonth + 1) + "/" + mDay + "/" + mYear; 
0

我看不出任何明显不正确的代码,只要你的数据库引用实例化好。如果可能,请将日期作为整数存储在数据库中,并将date.toTime()中的毫秒值存储起来。然后从新Date(毫秒)实例化日期对象变得更容易,然后可以根据区域设置格式化输出。

相关问题