-2

I have a database in My Application and i want to retrive data from database according to the Current Date.. I had used this query but when i click retrive button nothing shows up in listView

Select * from BluetoothDevice where Date = current_date

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 where Date = current_date;", null);

    while (c.moveToNext()) {
        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) {

}
}
Faiz Mir
  • 599
  • 5
  • 16

1 Answers1

1

You definitely need to put your created table into the onUpgrade method.

//Checks wether the old table has to be removed
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE BluetoothDevice");
    onCreate(db);
}

Furthermore you need to increment your DB_VERSION with every change you made at your database

Pepe Bellin
  • 85
  • 1
  • 12