2016-11-09 40 views
0

我正在创建一个数据库,我需要显示前一天的数据。如何在android sqlite中传递字符串而不是字符串参数

Long test= System.currentTimeMillis()-24*60*60; 

    DateFormat dateFormattest= DateFormat.getDateInstance(); 
    String dateFilter= dateFormattest.format(test); 
    Log.v("initial", dateFilter); 

    // String[] args={dateFilter.toString()}; 

    cursor=dba.query(Utils.Table_Name,new String[]{Utils.Id_Name,Utils.Food_Name,Utils.Calorie_Name, 
     Utils.Date_Name},Utils.Date_Name+"=?",new String[]{dateFilter},null,null,Utils.Date_Name+ " DESC"); 
    // Log.v("sentvalue", args+""); 

我在datefilter正确的值在 logcatbut的初始值月8,2016 但sentvalue的logcat的是Ljava.lang.String; @ 501eb6

回答

0

您正在尝试打印字符串这就是为什么logcat是Ljava.lang.String;@501eb6的数组对象。

请尝试像下面那样打印数组的第一个对象的值。

Log.v("sentvalue", args[0]); 

试着像下面那样查询。

String[] projection = {utils.Id_Name, 
         Utils.Food_Name, 
         Utils.Calorie_Name, 
         Utils.Date_Name}; 

String selection = Utils.Date_Name + "< ?"; 
String[] selectionArgs = new String[] { String.valueOf(System.currentTimeMillis()) }; 
String orderBy = Utils.Date_Name+ " DESC";  

Cursor cursor = db.query(
     Utils.Table_Name,       // The table to query 
     projection,        // The columns to return 
     selection,        // The columns for the WHERE clause 
     selectionArgs,       // The values for the WHERE clause 
     null,          // don't group the rows 
     null,          // don't filter by row groups 
     orderBy         // The sort order 
     ); 
+0

致命异常:主 工艺:com.example.shailendra.navtest,PID:24953 java.lang.IllegalArgumentException异常:由于索引超出范围不能在索引2结合参数。该语句有1个参数。 – rishav

+0

我想将其选为datefilter。我怎么把日期过滤器,因为它不采取datefilter参数...它要求字符串数组 – rishav

+0

你在数据库中插入'Date_Name'的格式?您应该使用该格式作为'selectionArgs'来从数据库中进行查询。 –