2016-09-08 120 views
-1

在我的數據庫表中,我有一個日期列和其他2列。
我想寫它檢索已根據系統保存日期today,數據的查詢。SQLite數據庫查詢

public class UserDbHelper extends SQLiteOpenHelper { 

    public static final String DATABASENAME = "DATABASENAME2"; 
    public static final int DB_VERSION = 1; 

    public UserDbHelper(Context context) 
    { 
     super(context,DATABASENAME,null,DB_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS BluetoothDevice (Device VARCHAR , Address VARCHAR , Date VARCHAR);"); 

    } 
    public List<BluetoothDevice> getItemFromDatabase(SQLiteDatabase sqLiteDatabase) { 
     List<BluetoothDevice> result = new ArrayList<>(); 
     // query database elements 
     Cursor c = sqLiteDatabase.rawQuery("Select * from BluetoothDevice;", null); 

     while (c.moveToNext()) { 
      Date v = new Date(); 
      v.setTime(c.getInt(c.getColumnIndex("Date")) * 1000); 
      Date date = new Date(); 
      date.setTime(Long.valueOf(c.getString(c.getColumnIndex("Date")))); 
      result.add(
        new BluetoothDevice(
          c.getString(c.getColumnIndex("Device")), 
          c.getString(c.getColumnIndex("Address")), 
          date 

        ) 
      ); 
     } 
     c.close(); 
     return result; 
    } 

    public void store(List<BluetoothDevice> data,SQLiteDatabase sqLiteDatabase) { 
     for (BluetoothDevice value : data) { 
      String s = String.valueOf(new Date().getTime()); 
      //insert in database 
      sqLiteDatabase.execSQL("INSERT INTO BluetoothDevice VALUES(?,?,?,?);", new String[]{value.name, value.address, s}); 

     } 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    } 

} 
+0

問題是什麼?如果您想** **的東西,不是請說明你已盡力讓這 –

+0

什麼,我想你想'c.getLong(c.getColumnIndex(「DATE」))' –

+0

我想根據從數據庫retrive數據當前日期 –

回答

0

首先得到日曆的實例。然後用它來獲得時間。

Calendar today = Calendar.getInstance(); 

然後使用getmethod從日曆實例中獲取當前時間。然後使用下面提到的數據庫方法替換您的查詢和選擇參數。

get(Calendar.HOUR_OF_DAY): 0 to 23 
get(Calendar.MINUTE): 0 to 59 
get(Calendar.SECOND): 0 to 59 

public Cursor getInformation(SQLiteDatabase db,string date){ 
    Cursor cursor; 
    String[] projections={NAME.OF.COLUMNS.YOU.WANT1, NAME.OF.COLUMNS.YOU.WANT2}; 
    String selection=COLUMN.NAME.OF.DATE+" LIKE ?" 
    String[] selection_args={date}; 
    cursor=db.query(TABLE_NAME,projections,selection,selection_args,null,null,null); 
    return cursor; 
} 
+0

但我沒有做一個單獨的列的可能的複製我在查詢 –

+0

我發佈了我的數據庫類 –