1.查询
范例1:查询某个表是否包含某个id:
public boolean isSaved( int ID) {
QueryBuilder<SaveList>
qb = saveListDao.queryBuilder();
qb.where(Properties.Id.eq(ID));
qb.buildCount().count();
return qb.buildCount().count()
> 0 ? true : false ; } |
范例2:获取整个表的数据集合,一句代码就搞定!
1
2
3
4
|
public List<PhotoGalleryDB>
getPhotoGallery() {
return photoGalleryDao.loadAll(); //
获取图片相册 } |
范例3:通过一个字段值查找对应的另一个字段值(为简便直接使用下面方法,也许有更简单的方法,尚未尝试)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** 通过图片id查找其目录id */ public int getTypeId( int picId) {
QueryBuilder<PhotoGalleryDB>
qb = photoGalleryDao.queryBuilder();
qb.where(Properties.Id.eq(picId));
if (qb.list().size()
> 0 )
{
return qb.list().get( 0 ).getTypeId();
}
else
{
return - 1 ;
} } |
范例4:查找所有第一姓名是“Joe”并且以lastname排序。
1
2
3
4
|
List joes = userDao.queryBuilder() .where(Properties.FirstName.eq( "Joe" )) .orderAsc(Properties.LastName) .list(); |
范例5:多重条件查询
(1)获取id为cityId并且infotype为HBContant.CITYINFO_SL的数据集合:
1
2
3
4
5
6
7
|
public List<CityInfoDB>
getSupportingList( int cityId) {
QueryBuilder<CityInfoDB>
qb = cityInfoDao.queryBuilder();
qb.where(qb.and(Properties.CityId.eq(cityId),Properties.InfoType.eq(HBContant.CITYINFO_SL)));
qb.orderAsc(Properties.Id); //
排序依据
return qb.list(); } |
(2)获取firstname为“Joe”并且出生于1970年10月以后的所有user集合:
1
2
3
4
5
|
QueryBuilder qb = userDao.queryBuilder(); qb.where(Properties.FirstName.eq( "Joe" ), qb.or(Properties.YearOfBirth.gt( 1970 ), qb.and(Properties.YearOfBirth.eq( 1970 ),
Properties.MonthOfBirth.ge( 10 )))); List youngJoes = qb.list(); |
范例6:获取某列对象
1
|
picJsonDao.loadByRowId(picId); |
2.增添/插入、修改
插入数据更加简单,也是只要一句代码便能搞定!
1
2
3
4
|
public void addToPhotoTable(Photo
p) {
photoDao.insert(p); } |
修改更新:
1
2
|
photoDao.insertOrReplace(photo); photoDao.insertInTx(photo); |
3.删除:
(1)清空表格数据
1
2
3
4
5
|
/** 清空相册图片列表的数据 */ public void clearPhoto() {
photoDao.deleteAll(); } |
(2)删除某个对象
1
2
3
4
5
6
|
public void deleteCityInfo( int cityId) {
QueryBuilder<DBCityInfo>
qb = cityInfoDao.queryBuilder();
DeleteQuery<DBCityInfo>
bd = qb.where(Properties.CityId.eq(cityId)).buildDelete();
bd.executeDeleteWithoutDetachingEntities(); } |
/**
* Created by pokawa on 18/05/15.
*/
public class MigrationHelper {
privatestatic final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION ="MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS";
private static MigrationHelperinstance;
public static MigrationHelper getInstance() {
if(instance ==null) {
instance = new MigrationHelper();
}
return instance;
}
public void migrate(SQLiteDatabase db, Class<?extends AbstractDao<?, ?>>... daoClasses) {
generateTempTables(db, daoClasses);
DaoMaster.dropAllTables(db, true);
DaoMaster.createAllTables(db, false);
restoreData(db, daoClasses);
}
private void generateTempTables(SQLiteDatabase db, Class<?extends AbstractDao<?, ?>>... daoClasses) {
for(int i = 0; i < daoClasses.length; i++) {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String divider = "";
String tableName = daoConfig.tablename;
String tempTableName = daoConfig.tablename.concat("_TEMP");
ArrayList<String> properties = new ArrayList<String>();
StringBuilder createTableStringBuilder = new StringBuilder();
createTableStringBuilder.append("CREATE TABLE ").append(tempTableName).append(" (");
for(int j = 0; j < daoConfig.properties.length; j++) {
String columnName = daoConfig.properties[j].columnName;
if(getColumns(db, tableName).contains(columnName)) {
properties.add(columnName);
String type = null;
try {
type = getTypeByClass(daoConfig.properties[j].type);
} catch (Exception exception) {
}
createTableStringBuilder.append(divider).append(columnName).append(" ").append(type);
if(daoConfig.properties[j].primaryKey) {
createTableStringBuilder.append(" PRIMARY KEY");
}
divider = ",";
}
}
createTableStringBuilder.append(");");
db.execSQL(createTableStringBuilder.toString());
StringBuilder insertTableStringBuilder = new StringBuilder();
insertTableStringBuilder.append("INSERT INTO ").append(tempTableName).append(" (");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(") SELECT ");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(" FROM ").append(tableName).append(";");
db.execSQL(insertTableStringBuilder.toString());
}
}
private void restoreData(SQLiteDatabase db, Class<?extends AbstractDao<?, ?>>... daoClasses) {
for(int i = 0; i < daoClasses.length; i++) {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
String tempTableName = daoConfig.tablename.concat("_TEMP");
ArrayList<String> properties = new ArrayList<String>();
for (int j = 0; j < daoConfig.properties.length; j++) {
String columnName = daoConfig.properties[j].columnName;
if(getColumns(db, tempTableName).contains(columnName)) {
properties.add(columnName);
}
}
StringBuilder insertTableStringBuilder = new StringBuilder();
insertTableStringBuilder.append("INSERT INTO ").append(tableName).append(" (");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(") SELECT ");
insertTableStringBuilder.append(TextUtils.join(",", properties));
insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");
StringBuilder dropTableStringBuilder = new StringBuilder();
dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);
db.execSQL(insertTableStringBuilder.toString());
db.execSQL(dropTableStringBuilder.toString());
}
}
private String getTypeByClass(Class<?> type)throws Exception {
if(type.equals(String.class)) {
return "TEXT";
}
if(type.equals(Long.class) || type.equals(Integer.class) || type.equals(long.class)) {
return "INTEGER";
}
if(type.equals(Boolean.class)) {
return "BOOLEAN";
}
Exception exception = new Exception(CONVERSION_CLASS_NOT_FOUND_EXCEPTION.concat(" - Class: ").concat(type.toString()));
throw exception;
}
private static List<String> getColumns(SQLiteDatabase db, String tableName) {
List<String> columns = new ArrayList<String>();
Cursor cursor = null;
try {
cursor = db.rawQuery("SELECT * FROM " + tableName +" limit 1", null);
if (cursor != null) {
columns = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
return columns;
}
}