gpt4 book ai didi

Android:如何使用 SugarORM 搜索数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:13 24 4
gpt4 key购买 nike

我不熟悉 Sqlite,因此正在使用数据库库 SugarORM,其文档位于 http://satyan.github.io/sugar/creation.html

它的大部分功能与sqlite中的相同,但使用更简单的命令。

练习数据库

public class ExerciseDB extends SugarRecord 
{
@Column(name = "ex_recordId", unique = true, notNull = true)
private String ex_recordId;
private String ex_group;
private String ex_name;
private int ex_cal;

public ExerciseDB()
{
}

public ExerciseDB(String ex_recordId, String ex_group, String ex_name, int ex_cal)
{
this.ex_recordId = ex_recordId;
this.ex_group = ex_group;
this.ex_name = ex_name;
this.ex_cal = ex_cal;
}

数据库保存示例如下:

ExerciseDB e0001= new ExerciseDB("ex0001", "Sports", "AAA", 190); save(e0001);
ExerciseDB e0002= new ExerciseDB("ex0002", "Sports", "BBB", 190); save(e0002);

问题:

请问如何查找“ex0001”记录?我尝试使用以下代码进行搜索:

ExerciseDB.find(ExerciseDB.class, key);
List<ExerciseDB> books = ExerciseDB.find(ExerciseDB.class, "ex_recordId = ?", key, null,null,null);
String gg = books.get(1).get_ex_group();

但是它报告

android.database.sqlite.SQLiteException: no such column: ex0001 (code 1): , while compiling: SELECT * FROM Ex_Records WHERE ex0001

查找的官方文档:

List<Book> books = Book.find(Book.class, "author = ?", new String{author.getId()});

Book book = Book.findById(Books.class, 1);
Author author = book.author;

但不知道这个1代表什么以及如何找到

代码文档如下:

    public static <T> List<T> find(Class<T> type, String whereClause, String... whereArgs) {
return find(type, whereClause, whereArgs, null, null, null);
}

public static <T> List<T> find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
T entity;
List<T> toRet = new ArrayList<T>();
Cursor c = sqLiteDatabase.query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
groupBy, null, orderBy, limit);
try {
while (c.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
SugarRecord.inflate(c, entity);
toRet.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}
return toRet;
}

基础应该和Sqlite差不多。。。有没有人可以根据上面的代码文档提供帮助?非常感谢!

最佳答案

我想您现在可能已经找到了答案,但对于那些遇到这个问题并想知道的人来说。

Sugar 格式化列名:“read”变为“READ”,isRead 变为“IS_READ”。所以你可以使用你的 SugarRecord.find()。您只需要弄清楚 sugar 如何格式化您的列名“ex_recordId”。我认为它将格式化为 EX_RECORD_ID 或 EXRECORD_ID。

关于Android:如何使用 SugarORM 搜索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010470/

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