简体   繁体   中英

I get no results when running an sqlite query on android with a where clause

I'm writing an app to track two numbers entered by a user for a given date. The project is kind of a learning exercise for me and also something for my SO to use for his job. Using the database inspector in android studio, I'm able to see that the database has values:

| Date (String) | morningodometer (REAL)| eveningodometer (REAL) |
| ------------- | --------------------- | ---------------------- |
| 2021-01-01    | 42.0                  | 56.9                   |

I have a query to then get the data from the table, where the date = 2021-01-01 , but my query is returning 0 entries. My database definition/helper is here:

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "BEEPBEEP";

    public static final String _ID = "_id";
    public static final String DATE = "date";
    public static final String MORNING_ODOMETER = "morningodometer";
    public static final String EVENING_ODOMETER = "eveningodometer";

    static final String DB_NAME = "BEEPBEEP.DB";

    static final int DB_VERSION = 1;

    private static final String CREATE_TABLE = "create table " + TABLE_NAME + " (" + DATE +
            " TEXT PRIMARY KEY, " + MORNING_ODOMETER + " REAL NOT NULL, " + EVENING_ODOMETER +
            " REAL NOT NULL);";

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

And my code for getting the numbers, based on the date is here:

    public Optional<DayOdometer> getOdometerForDate(Date date) {
        String[] columns = new String[] {DatabaseHelper.MORNING_ODOMETER, DatabaseHelper.EVENING_ODOMETER};
        String formattedDate = dateFormatter.format(date);
        String selection = "?=?";
        String[] selectionArgs = {DatabaseHelper.DATE, formattedDate};
        Cursor cursor = dbHelper.getReadableDatabase().query(DatabaseHelper.TABLE_NAME,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null,
                null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            float eveningKms = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.EVENING_ODOMETER));
            float morningKms = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.MORNING_ODOMETER));
            cursor.close();
            return Optional.of(new DayOdometer(morningKms, eveningKms));
        }
        return Optional.empty();
    }

I've put debugging statements in and popups in the app showing the date used for the selection args and it visually matched the database entry. So then I removed my selection criteria and compared the date in the returned cursor with the formattedDate String and it said that they were equal. I'm kinda stumped here as to what I'm doing wrong.

You can't use a ? placeholder for a column's name, only for parameter values.
Change to this:

String selection = DatabaseHelper.DATE + "=?";
String[] selectionArgs = {formattedDate};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM