Igr
Igr

Reputation: 1045

Error in sqlite query, android

I got this code launching a query on my database:

public List<Photo> getPhotos(Calendar from, Calendar to) {
    // Check if the Connection to the DB is open
    raiseConnectionExceptionIfNotConnected();

    ArrayList<Photo> photos = new ArrayList<Photo>();
    // Query the database
    Cursor cur = db.rawQuery(
        "SELECT " + Day_TABLE + "." + DAY_PHOTO +
        " FROM " + Day_TABLE +
        " WHERE " + Day_TABLE + "." + DAY_DATE + " BETWEEN =? AND =? " +
            " AND " + Day_TABLE + "." + DAY_PHOTO + " IS NOT NULL " +
        " ORDER BY " + Day_TABLE + "." + DAY_DATE + " DESC",
        new String[] {date_format.format(from.getTime()),
                      date_format.format(to.getTime()) }
    );

    if (cur.moveToFirst()) {
        do {
            photos.add(new Photo(cur.getString(0)));
        }
        while (cur.moveToNext());
    }

    cur.close();
    return photos;
}

But every time my activity calls it, I get this error:

06-30 17:09:46.488: E/AndroidRuntime(273): FATAL EXCEPTION: main 06-30 17:09:46.488: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.github.groupENIGMA.journalEgocentrique/com.github.groupENIGMA.journalEgocentrique.GalleryActivity}: android.database.sqlite.SQLiteException: near "=": syntax error: , while compiling: SELECT day.photo FROM day WHERE ( day.date BETWEEN =? AND =? ) ( AND day.photo IS NOT NULL ) ORDER BY day.date DESC

I'm not getting what the issue could be. The query seems ok to me...

Upvotes: 2

Views: 95

Answers (2)

Aaron
Aaron

Reputation: 11

The mistake are the "=" in the BETWEEN statement.

You should notice it in the log cat

near "=": syntax error:

Upvotes: 1

Ken Wolf
Ken Wolf

Reputation: 23269

You shouldn't use the = operator when using BETWEEN.

The correct syntax is

BETWEEN ? AND ? 

Upvotes: 3

Related Questions