greendao

greenDAO讲义(一):使用篇

♀尐吖头ヾ 提交于 2019-12-10 00:38:02
目前android开发刚学习了一个多月,最近开始研究三方开源框架的用法。 了解android开发的人应该都会知道,android的数据库开发主要用到sqlite(如果这点你不清楚,那这篇文章就直接pass吧)。 greenDAO应该算是当前最火的数据库开源框架了吧,它是一个移动开发的ORM(object / relational mapping)框架,至于ORM是什么,可以百度之,本人理解也不是很深,大概意思就是为懒人设计的能够将对象和关系以映射的方式表达出来。greenDAO就是如此: greenDAO will do the word for you: it maps Java objects to datebase tables(often called ORM). 这样开发人员就可以吧精力集中在软件开发上,减轻了"wrting sql and parsing query results" 等等这些"quite tedious tasks". 总之,一句话,greenDAO就是实现Java对象和SQLite Datebase的一个媒介人,简化了SQLite的操作。 注:官方网站 http://greendao-orm.com/ 1. 下载greenDAO 要使用肯定要先下载他的软件包了,官网上有它的连接,对于marven和gradle环境直接到serarch.maven

Android中GreenDao对数据库进行升级操作笔记

你。 提交于 2019-12-09 23:26:36
一、修改app中build.gradle的greendao的配置的schemaVersion greendao { //指定数据库schema版本号,迁移等操作会用到 schemaVersion 1 //通过gradle插件生成的数据库相关文件的包名,默认为你的entity所在的包名 daoPackage 'com.companyName.projectName.dao' /*这就是我们上面说到的自定义生成数据库文件的目录了, 可以将生成的文件放到我们的java目录中, 而不是build中,这样就不用额外的设置资源目录了*/ targetGenDir 'src/main/java' } 二、修改实体类 添加或删除实体类的属性 @Entity public class User { @Id private Long id; @Property private String userName; @Property private String password; @Property private String email;//新增属性 } 一般的数据库升级这样就可以了,特殊情况可能需要自己编写数据库迁移脚本,这种时候可以自定义DBHelper,定义方式如下,注意继承类: public class DBHelper extends DaoMaster.OpenHelper{

GreenDAO: store list of entities in other entity

久未见 提交于 2019-12-09 05:59:39
问题 I am playing around with GreenDAO and I think I like the way it works. I do not like to write SQL code, so this will help me avoid it ;) But still, I think it really is very 'sql'-based thinking of how you set it up. Not sure if this is a bad thing (to know how things work), but I'd rather just say: here is my object! Store it! But I haven't found anything for this yet... But ok, what I am doing is sort of the following: I have an object, let's say a bookshelve. I put some books on the shelve

How do I execute “select distinct ename from emp” using GreenDao

我只是一个虾纸丫 提交于 2019-12-09 03:13:54
问题 How do I execute "select distinct ename from emp" using GreenDao I am trying to get distinct values of a column of sqlite DB using GreenDao. How do I do it? Any help appreciated. 回答1: You have to use a raw query for example like this: private static final String SQL_DISTINCT_ENAME = "SELECT DISTINCT "+EmpDao.Properties.EName.columnName+" FROM "+EmpDao.TABLENAME; public static List<String> listEName(DaoSession session) { ArrayList<String> result = new ArrayList<String>(); Cursor c = session

Connection pool has been unable to grant a connection to thread

北战南征 提交于 2019-12-08 23:03:07
问题 I'm using GreenDAO for database handling in Android. When performing many database changes (> 15.000) I get this error Message: The connection pool for database '/data/data/...' has been unable to grant a connection to thread 312 (Thread-312) with flags 0x1 for 30.000002 seconds. Everything gets stuck. Why does this error happen? 回答1: I can't say for sure about this particular implementation, but there is a connectionpool usually backing a ORM. The connection pool opens a set number of

How to use join query for displaying data from multiple tables in greenDAO?

两盒软妹~` 提交于 2019-12-08 12:55:21
问题 Following is my DAOgenerator class: public class MyDaoGenerator { public static void main(String args[]) throws Exception { Schema schema = new Schema(3, "Dao"); Entity employee = schema.addEntity("Employee"); employee.addIdProperty().autoincrement(); employee.addStringProperty("name"); employee.addStringProperty("mobile"); employee.addStringProperty("address"); employee.addStringProperty("company_id"); Entity company = schema.addEntity("Company"); company.addIdProperty().autoincrement();

Android - Green Dao Multiple Transaction

我只是一个虾纸丫 提交于 2019-12-08 09:14:25
问题 I have some problem for multiple transaction in greendao example i already have two table with name book and type_book. i already have code like this : SQLiteDatabase db = bookMasterDao.getDatabase(); db.beginTransaction(); try { bookMasterDao.insert(bookMaster); idBook = bookMaster.getId().intValue(); db.setTransactionSuccessful(); } catch (Exception ex) { System.out.println("Error insert book master " + ex); } finally { db.endTransaction(); } SQLiteDatabase dbTypeBook = typeBookMasterDao

Multiple toMany relationships to a single table

喜夏-厌秋 提交于 2019-12-07 06:26:00
问题 I'm new to greenDAO and I'm working on writing the DaoGenerator. One issue that I've run into is that I have a user table and a wallpost table. I would like to be able to have two columns in the wallpost table that are toMany relations to the user table (the wall owner and the posting user) they may or may not be the same user, but so far it doesn't look like it is possible to have two toMany relations that point to a single table in the same table. Is there a better way to do this/a way to

GreenDao asynchronously loadAll methood

爷,独闯天下 提交于 2019-12-07 01:16:30
I can successfully insert rows asynchronously using GreeDAO 's AsyncSession like this: getMyObjectDao().getSession().startAsyncSession().insertOrReplaceInTx(MyObject.class, list); How can I load all objects from db into ArrayList asynchronously. So far I have tried below code but its not working: 1- <List>items = getBoxDao(c).getSession().startAsyncSession().loadAll(MyObject.class) ; 2- @Override public void onAsyncOperationCompleted(AsyncOperation operation) { String operationIs = null; switch (operation.getType()) { case LoadAll: itemsList = BoxRepository.getAllBoxes(getApplicationContext())

What is the best way of creating greenDAO DB connection only once for single run of application?

删除回忆录丶 提交于 2019-12-06 10:22:25
问题 Currently I am creating the greenDAO DB connection in a class (which opens the connection in every static method) and using it wherever I need it. But I am not sure if it's the best way of doing it. Can anyone suggest a better way of doing it? My Code: import com.knowlarity.sr.db.dao.DaoMaster; import com.knowlarity.sr.db.dao.DaoMaster.DevOpenHelper; import com.knowlarity.sr.db.dao.DaoSession; import com.knowlarity.sr.db.dao.IEntity; public class DbUtils { private static Object lockCallRecord