开发者

Problem in executing query using ormlite in android

I'm using the program found on this programming blog for using pre-populated sqlite table. I am trying to insert any row in the table but it is giving always SQLException:

Problems executing Android query: SELECT * FROM `user` WHERE `username`='Shadow'

This is the method where I'm getting this error:

public User getByUsername(String username) {        
    try {
        QueryBuilder<User, String> qb = userDao.queryBuilder(); 
        qb.where().eq("username", username);    
        PreparedQuery<开发者_Go百科User> pq = qb.prepare();
        return userDao.queryForFirst(pq);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
} 

Looking for help...


Without more information about the specific error you are getting (please add the full exception to your question), we can't really help you @deepak

I will say that your code looks well formed and appropriate. I like to define the fields that I use in queries like the following:

@DatabaseTable(tableName = "users")
public class User @{
    public static final String USERNAME_FIELD_NAME = "username"; 

    ...
    @DatabaseField(columnName = USERNAME_FIELD_NAME)
    private String userName;
    ...

Then your code can be something like the following. This solves problems with queries and database schemas not matching:

qb.where().eq(User.USERNAME_FIELD_NAME, username);    

If you post your exception, I'll edit my answer with more help.


Dont forget to write the default constructor of your Entity Class. In my case i forgot to write the constructor and was getting the same exception.

public User(){

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜