的。不管你之前是使用SQLiteOpenHelper还是第三方库来进行数据库操作,本文都非常适合你。
public class Book {
public Long _id;
public String title;
public Author author;
public Date publishDate;
}
接下来,我们将Book这个实体存储到数据库中。(变量名对应数据库中的字段名) 使用CupBoard来操作数据库,要用withDatabase()这个方法,并且CupBoard要求须提前将实体注入到SQLiteOpenHelper,我们使用
static initializer block 在这里也许是最好的方法。下面看例子:
首先创建数据库:
public class CupboardSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myapp.db";
private static final int DATABASE_VERSION = 1;
static {
// register our models
cupboard().register(Book.class);
cupboard().register(Author.class);
}
public CupboardSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// this will ensure that all tables are created
cupboard().withDatabase(db).createTables();
// add indexes and other database tweaks
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// this will upgrade tables, adding columns and new tables.
// Note that existing columns will not be converted
cupboard().withDatabase(db).upgradeTables();
// do migration work
}
}
需要注意的是, 从上面代码看到,我们只添加了一个static初始化块和
cupboard().withDatabase(db).createTables();
cupboard().withDatabase(db).upgradeTables();
而没有任何的继承(extends)和重写(override)。如何你使用的不是SQLiteOpenHelper,同样你也要添加一个static初始化块和update、upgrade
存储Objects
存储数据时使用withDatabase(db).put()方法。
Book book = ...
long id = cupboard().withDatabase(db).put(book);
读取Objects
通过id获得一条数据:读取id为12的数据对象,如果没有返回null。
Book book = cupboard().withDatabase(db).get(Book.class, 12L);
我们还可以使用query()来遍历数据库
// get the first book in the result
Book book=cupboard().withDatabase(db).query(Book.class).get();
// Get the cursor for this query
Cursor books=cupboard().withDatabase(db).query(Book.class).getCursor();
try{
// Iterate books
QueryResultIterableitr=cupboard().withDatabase(db).query(Book.class).iterate();
for(Book book:itr){
// do something with book
}
}finally{
// close the cursor
itr.close();
}
// Get the first matching book with title Android
Book book=cupboard().withDatabase(db).query(Book.class).withSelection("title = ?","Android").get();
更改Objects
要是更改整个实体对象,我们可以用put()方法,若是更改一个实体的部分或者是一次更改多个实体,这时使用update().
ContentValues values = new ContentValues(1);
values.put("title", "Android")
// update all books where the title is 'android'
cupboard().withDatabase(db).update(Book.class, values, "title = ?", "android");
删除Objects
删除和读get()、写put()一样简单
// by id
cupboard().withDatabase(db).delete(Book.class, 12L);
// by passing in the entity
cupboard().withDatabase(db).delete(book);
// or by selection
cupboard().withDatabase(db).delete(Book.class, "title = ?", "android");
提示
与技巧
1、某些情况下,如果你需要调用SQLiteDatabase,可以将任何注册过的实体转换为ContentValues对象
ContentValues values = cupboard().withEntity(Book.class).toContentValues(book);
// you can also reuse ContentValues
values = cupboard().withEntity(Book.class).toContentValues(book, values);
2、读一个数据库进行多个操作
public void doDatabaseWork(SQLiteDatabase database, Book book) {
DatabaseCompartment dbc = cupboard().withDatabase(database);
dbc.put(book);
dbc.update(Book.class, "title = ?", "android");
}
3、下载Jar or 使用Maven
nl.qbusict
cupboard
1.0.6
Gradle
:
compile 'nl.qbusict:cupboard:1.0.6'
更多内容:https://bitbucket.org/Jabin/cupboard or blog.csdn.net/zjbpku