问题
How can we rename column name in ORMLite?
I am trying to write that query SELECT id as _id from some_table
Android Cursor Adapter requires us to have column named _id
and ORMLite
requires us to have column named id
.
I am trying to write that query and return Cursor
from this query.
Dao<NewsArticle, Long> newsArticleDao =
((SomeApp)mContext.getApplicationContext()).getDAOConnection().getDAO(
NewsArticle.class);
QueryBuilder<NewsArticle, Long> query = newsArticleDao.queryBuilder().selectRaw(
"`id` as `_id`");
PreparedQuery<NewsArticle> preparedQuery = query.prepare();
CloseableIterator<NewsArticle> iterator = newsArticleDao.iterator(preparedQuery);
AndroidDatabaseResults results =
(AndroidDatabaseResults)iterator.getRawResults();
cursor = results.getRawCursor();
That's what I have so far but I am getting this error when I pass query to iterator.
java.sql.SQLException: Could not compile this SELECT_RAW statement since the caller is expecting a SELECT statement. Check your QueryBuilder methods.
回答1:
I guess I have to answer my question.
I still couldn't figure out how to rename column but I found the solution to problem.
Android Custom Adapter
requires column named_id
that's true but ORMLite
doesn't require the column name to be id
, I was wrong about that. It wants you to mark a column as id.
In my model, I marked my id and now it works like a charm.
@DatabaseField(id = true)
private long _id;
回答2:
How can we rename column name in ORMLite?
Not sure I understand what you mean by rename. I don't think you mean schema change here. Are you talking about how to assign the name of the field in the database? You can do:
@DatabaseField(id = true, columnName = "_id")
private long id;
ORMLite requires us to have column named id.
Uh, no. ORMLite shouldn't care what the name of your column is. Maybe you are talking about Android?
I am trying to write that query and return Cursor from this query.
Is this is a different question?
java.sql.SQLException: Could not compile this SELECT_RAW statement since the caller is expecting a SELECT statement. Check your QueryBuilder methods.
You are getting this because you are calling:
newsArticleDao.queryBuilder().selectRaw(...);
And then calling:
query.prepare();
To quote from the javadocs of selectRaw(...)
... This will turn the query into something only suitable for the Dao.queryRaw(String, String...) type of statement.
I think you want to assign the name of your column using columnName
. Have you looked at the javadocs for @DatabaseField? Or maybe look up "column name" in the online manual?
来源:https://stackoverflow.com/questions/21096426/how-to-rename-columns-in-ormlite