Android: 如何使用 SugarORM 搜索数据
Android: How to search data using SugarORM
我不熟悉 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差不多。。。有没有人可以根据上面的代码文档提供帮助?非常感谢!
我用它的另一种方法findWithQuery
如下:
List<ExerciseDB> bb = ExerciseDB.findWithQuery(ExerciseDB.class, "Select * from Ex_Records where ex_recordId = ?", "ex0001");
我想你现在可能已经找到了答案,但对于遇到这个问题并想知道的人来说。
Sugar 格式化列名:"read" 变为 "READ",isRead 变为 "IS_READ"。所以你可以使用你的SugarRecord.find()。您只需要弄清楚 sugar 如何格式化您的列名 "ex_recordId"。我认为它会将其格式化为 EX_RECORD_ID 或 EXRECORD_ID.
我不熟悉 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差不多。。。有没有人可以根据上面的代码文档提供帮助?非常感谢!
我用它的另一种方法findWithQuery
如下:
List<ExerciseDB> bb = ExerciseDB.findWithQuery(ExerciseDB.class, "Select * from Ex_Records where ex_recordId = ?", "ex0001");
我想你现在可能已经找到了答案,但对于遇到这个问题并想知道的人来说。
Sugar 格式化列名:"read" 变为 "READ",isRead 变为 "IS_READ"。所以你可以使用你的SugarRecord.find()。您只需要弄清楚 sugar 如何格式化您的列名 "ex_recordId"。我认为它会将其格式化为 EX_RECORD_ID 或 EXRECORD_ID.