ORMLite id in rawQuery
Can an alias (AS) be used in a query for ORMLite in Android? I am trying to use it with the following code:
String query =
"SELECT *, (duration - elapsed) AS remaining FROM KitchenTimer ORDER BY remaining";
GenericRawResults<KitchenTimer> rawResults =
getHelper().getKitchenTimerDao().queryRaw(
query, getHelper().getKitchenTimerDao().getRawRowMapper());
But when these codes are executed, it throws the following error:
java.lang.IllegalArgumentException: Unknown column name 'remaining' in table kitchentimer
source to share
java.lang.IllegalArgumentException: Unknown column name "Remaining" in kitchentimer table
The raw-row-mapper transformer associated with yours KitchenTimerDao
expects the results to match the columns directly KitchenTimer
. However, since you add your column remaining
, it doesn't need to put that result column, hence the exception. This is a raw query, so you will need to come up with your own results mapper - you cannot use DAO. See docs for pending requests .
For example, if you want to map the results to your own object Foo
, you can do something like:
String query =
"SELECT *, (duration - elapsed) AS remaining FROM KitchenTimer ORDER BY remaining";
GenericRawResults<Foo> rawResults =
orderDao.queryRaw(query, new RawRowMapper<Foo>() {
public Foo mapRow(String[] columnNames, String[] resultColumns) {
// assuming 0th field is the * and 1st field is remaining
return new Foo(resultColumns[0], Integer.parseInt(resultColumns[1]));
}
});
// page through the results
for (Foo foo : rawResults) {
System.out.println("Name " + foo.name + " has " + foo.remaining + " remaining seconds");
}
rawResults.close();
source to share
I have the same problem. I wanted to get a list of objects, but added a new attribute with an alias.
To continue using the object mapper from OrmLite, I used RawRowMapper to get columns and results. But instead of converting all the columns manually, I first read the alias and remove its reference in the column arrays. Then the OrmLite Dao cartographer can be used.
I write this in Kotlin code:
val rawResults = dao.queryRaw<Foo>(sql, RawRowMapper { columnNames, resultColumns ->
// convert array to list
val listNames = columnNames.toMutableList()
val listResults = resultColumns.toMutableList()
// get the index of the column not included in dao
val index = listNames.indexOf(ALIAS)
if (index == -1) {
// There is an error in the request because Alias was not received
return@RawRowMapper Foo()
}
// save the result
val aliasValue = listResults[index]
// remove the name and column
listNames.removeAt(index)
listResults.removeAt(index)
// map row
val foo = dao.rawRowMapper.mapRow(
listNames.toTypedArray(),
listResults.toTypedArray()
) as Foo
// add alias value. In my case I save it in the same object
// but another way is to create outside of mapping a list and
// add this value in the list if you don't want value and object together
foo.aliasValue = aliasValue
// return the generated object
return@RawRowMapper foo
})
This is not the shortest solution, but it is very important for me to keep using the same cartographers. This avoids errors when adding an attribute to the table, and you don't forget to update the display.
source to share