gpt4 book ai didi

android - ORMLite 不能做 queryForId

转载 作者:太空狗 更新时间:2023-10-29 15:21:14 25 4
gpt4 key购买 nike

我是 android 和 ormLite 的新手,我只是尝试存储数据并使用 queryForId 再次取回数据。起初它运行良好,但不知何故在摆弄了一下之后......我得到异常说我的类(class)没有 id 字段。

这是类的代码

@DatabaseTable(tableName="ForeignData")
public class ForeignData implements Serializable {

private static final long serialVersionUID = 3640428963380696279L;

@DatabaseFieldId(generatedId = true)
private Integer id;

@DatabaseFieldSimple(defaultValue = "")
private String name;

public ForeignData(){};
}

我就是这样调用它的...

ForeignData f = new ForeignData();
try {
Dao<ForeignData, Integer> fdao = new DatabaseHelper(getApplicationContext()).getForeignDao();
f.setName("test");
fdao.create(f);
f = fdao.queryForId(f.getId())==null?f:fdao.queryForId(f.getId());
txt1.setText(f.getName());
} catch (SQLException e) {
txt1.setText(e.getMessage());
}

它在

处捕获异常
f = fdao.queryForId(f.getId())==null?f:fdao.queryForId(f.getId());

因为 ForeignData 没有 id 列,所以返回不能查询 id。

如果我将 ForeignData 类中的 id 更改为

 @DatabaseField(generatedId=true,columnName="id")
private Integer id;

我得到的异常是“queryForOne from database field: SELECT * FROM 'ForeignData' WHERE 'id'=?”

最佳答案

您可以将 ForeignData 类中的 id 更改为:

@DatabaseField(id = true)
private Integer id;

关于android - ORMLite 不能做 queryForId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com