0

I want to create a very simple query to look up a sqlite db using greendao. 2 fields, one is the ID and the other 'affirmation'. i am sorry to be such a beginner, but i am not sure how to use greendao including what to import etc.. All i have been able to do so far is add the greendao libraries but i cant find a good tutorial to just do a query. Basically i want it to be a random ID that calls up a random affirmation and return it to my main activity.. Once again i am sorry but i am really trying and getting nowhere..

sunirmalya
  • 67
  • 2
  • 9
  • Here is a good tutorial on how to use GreenDAO: http://blog.surecase.eu/using-greendao-with-android-studio-ide/. It shows how to insert an ID for your entries as well. – IgorGanapolsky Jun 10 '14 at 19:39
  • Check this post http://wiki.workassis.com/android-greendao-example/ – Bikesh M Sep 28 '16 at 10:07

3 Answers3

1

Greendao is a ORM-framework. If you don't know what this means you should look up this first.

Greendao generally works as follows:

  • You create a java-project that generates your sourcecode for your real app. You have to include DaoCore and DaoGenerator in this project.
  • You add the generated sourcecode to your android-project and include DaoCore in it. DaoGemerator is not neccessary.

For examples how to generate the code and define your entities the greendao-website is a good place to go.

According to your description you need an entity with id-property and a string-property (affirmation).

In your android-project you then use the DevOpenHelper to get a session and from the session you can get the dao (Data Access Object) for your entity. The dao includes the very basic query to load data by id (load ()).

Please notice that the DevOpenHelper is only meant for development process. For your final release you should extend OpenHelper and costumize your actions to be taken on DB-schema update.

AlexS
  • 5,295
  • 3
  • 38
  • 54
0

Here is some example code I have in my application.

DaoHelper.getInstance().getDaoSession().clear(); 
OperationDao dao = DaoHelper.getInstance().getDaoSession().getOperationDao();
String userId = "some id"
WhereCondition wc1 = new WhereCondition.PropertyCondition(OperationDao.Properties.UserId,
                " = " + userId);
WhereCondition wc2 = new WhereCondition.PropertyCondition(OperationDao.Properties.Priority,
            " > " + 4);
// Uncached is important if your data may have changed recently.
List<Operation> answer = dao.queryBuilder().where(wc1, wc2).listLazyUncached();
Arcantos
  • 1,052
  • 1
  • 13
  • 28
0

This is a decent tutorial on how to learn greendao. Make sure you follow the links to the further parts.

You can use:

daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
yourDao = daoSession.getYourDao();

Random() random = new Random();
List<YourObject> objects = yourDao.loadAll();
YourObject yourObject = objects.get(random.nextInt(objects.size());
Mike Porter
  • 70
  • 2
  • 7