简体   繁体   中英

How to include arguments in a where-clause in Android SQLite?

I have a table T1, which has a float column representing the julianday. Now I want to delete the entries which julianday is older than n-day. I tried:

db.delete(T1, COL_JULIANDAY + " <= julianday('now', '- ? days')",
  new String[] { Integer.toString(days) });

But I got an error from Android:

 android.database.sqlite.SQLiteException: bind or column index out of range

I think it is because the '?' mark is quoted in my where-clause.

Rather than resorting to raw SQL, I suggest trying to use concatenation operators in your where clause.

db.delete(T1, COL_JULIANDAY + " <= julianday('now', '- ' || ? || ' days')",
    new String[] { Integer.toString(days) });

I haven't checked it yet but this is how I think it should be,

db.delete(T1, COL_JULIANDAY + " <= julianday(?)", new String[] 
                                                    { Integer.toString(days) });

If you prefer you can simply use sql query with db.execSQL(statement);

db.execSQL("delete from "+T1+" where COL_JULIANDAY <= julianday('"+days+"')");
julianday('now', '- ? days')

应该与

julianday('now') - ?

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