SQLiteDatabase.query不替换?s

时间:2013-12-25 20:06:46

标签: android sql sqlite where-clause

似乎SQLiteDatabase.query()方法没有用chooseArgs替换where子句中的?:

String[] columns = new String[] { Table.COL_WEIGHT };
String[] selectionArgs = new String[] { Table.COL_DATE, Table.COL_DATE,
        Table.TABLE_NAME };

String sqlquery = SQLiteQueryBuilder.buildQueryString(false, Table.TABLE_NAME,
        columns, "?=(SELECT max(?) FROM ?)", null, null, null, null);

Cursor cursor = mDb.rawQuery(sqlquery, selectionArgs);

这引发了以下异常:

FATAL EXCEPTION: main
java.lang.RuntimeException:
Unable to start activity ComponentInfo{Activity}:
android.database.sqlite.SQLiteException: near "?":
syntax error (code 1): , while compiling:
SELECT Weight FROM Bodyfat WHERE ?=(SELECT max(?) FROM ?)

虽然:

String sql = String.format("SELECT %s FROM %s WHERE %s=(SELECT max(%s) FROM %s)",
        Table.COL_WEIGHT, Table.TABLE_NAME, Table.COL_DATE,
        Table.COL_DATE, Table.TABLE_NAME);
//SELECT Weight FROM Bodyfat WHERE Date=(SELECT max(Date) FROM Bodyfat)


Cursor cursor = mDb.rawQuery(sql, null);

像预期的那样工作。我是否正确使用query()和selectionArgs?

0 个答案:

没有答案