在android中使用ormlite执行查询时出现问题

发布于 2024-11-30 15:01:00 字数 738 浏览 4 评论 0原文

我正在使用 此编程博​​客用于使用预填充的 sqlite 表。我试图在表中插入任何行,但它总是给出 SQLException:

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

这是我收到此错误的方法:

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

寻求帮助...

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<User> pq = qb.prepare();
        return userDao.queryForFirst(pq);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
} 

Looking for help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2024-12-07 15:01:00

如果没有有关您收到的特定错误的更多信息(请在您的问题中添加完整的异常),我们无法真正帮助您@deepak,

我会说您的代码看起来格式良好且合适。我喜欢定义在查询中使用的字段,如下所示:

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

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

然后您的代码可以如下所示。这解决了查询和数据库模式不匹配的问题:

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

如果您发布异常,我将在更多帮助下编辑我的答案。

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.

☆獨立☆ 2024-12-07 15:01:00

不要忘记编写实体类的默认构造函数。就我而言,我忘记编写构造函数并得到相同的异常。

public User(){

}

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(){

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文