我不熟悉 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
find 的官方文档:
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差不多……有没有人可以根据上面的代码文档提供帮助?提前谢谢了!