gpt4 book ai didi

android - 如何在 OrmLite 中获取列/字段名称?

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:21 24 4
gpt4 key购买 nike

我通过通常的注释定义表/字段名称,例如:

@DatabaseTable(tableName = "M")
public class Headers {
//android requires primary key as _ID
@DatabaseField(generatedId = true, columnName = "_ID", dataType = DataType.INTEGER_OBJ)
private Integer id;

@DatabaseField(
columnName = "MM2",
uniqueCombo = true,
canBeNull = true, //null means root folder
dataType = DataType.INTEGER_OBJ
)
private Integer parentId;
}

与此同时,在我的代码深处的某个地方,我需要进行一些查询,例如:

QueryBuilder<Headers, Integer> qb = headersDao.queryBuilder();
qb.where().eq("_ID", headerId); //_ID field name

这种代码看起来很难看,因为字段名称(在本例中为 _ID)是硬编码的(即使声明为 final static String)

问题:这是正常的 OrmLite 编码实践吗?我希望我可以使用一些小代码来代替硬编码的 _ID 字段。 OrmLite 肯定“知道”所有字段名称。谁能帮帮我?

最佳答案

为字段名定义一个常量,如

private static final String FIELD_ID = "_ID";
// Or make it public as Gray said into comment
public static final String FIELD_ID = "_ID";

像这样使用它

//android requires primary key as _ID
@DatabaseField(generatedId = true, columnName = FIELD_ID, dataType = DataType.INTEGER_OBJ)
private Integer id;

进入你的查询

QueryBuilder<Headers, Integer> qb = headersDao.queryBuilder();
qb.where().eq(FIELD_ID, headerId); //_ID field name

如果你看documentation

他们也遵循相同的

QueryBuilder<Account, String> qb = accountDao.queryBuilder();
Where where = qb.where();
// the name field must be equal to "foo"
where.eq(Account.NAME_FIELD_NAME, "foo");
// and
where.and();
// the password field must be equal to "_secret"
where.eq(Account.PASSWORD_FIELD_NAME, "_secret");
PreparedQuery<Account, String> preparedQuery = qb.prepareQuery();

关于android - 如何在 OrmLite 中获取列/字段名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670972/

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