2016-11-09 72 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'來從數據庫中進行查詢。 –

相關問題