如何在GreenDao中使用DISTINCT [英] how to use DISTINCT in GreenDao

查看:1145
本文介绍了如何在GreenDao中使用DISTINCT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SqlLite中查询不同的行,我没有得到如何使用Android的GreenDao库进行查询.

I want to query distinct rows in SqlLite, I am not getting how to query using GreenDao library for android.

 List<activity> activities = activityDao.queryRaw("SELECT DISTINCT "+Properties.Date_time +" FROM "+activityDao.getTablename()+" WHERE "+Properties.Contact_number+ " = "+phonenumber);

错误:

11-13 21:12:50.915 9320-10428/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
                                              Process: com.ficean.android.ficean, PID: 9320
                                              java.lang.RuntimeException: An error occured while executing doInBackground()
                                                  at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                  at java.lang.Thread.run(Thread.java:818)
                                               Caused by: net.sqlcipher.database.SQLiteException: near "SELECT": syntax error: , while compiling: SELECT T."_id",T."FILE_UNIQUE_ID",T."CONTACT_NUMBER",T."POND_NAME",T."REMOTE_PATH",T."LOCAL_PATH",T."IN_OUT",T."SEEN",T."STATUS",T."DATE_TIME" FROM "ACTIVITY" T SELECT DISTINCT org.greenrobot.greendao.Property@176aa5bf FROM ACTIVITY WHERE org.greenrobot.greendao.Property@27a2f48c = +919063809087
                                                  at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
                                                  at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
                                                  at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
                                                  at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:83)
                                                  at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:49)
                                                  at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
                                                  at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1758)
                                                  at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1723)
                                                  at org.greenrobot.greendao.database.EncryptedDatabase.rawQuery(EncryptedDatabase.java:32)
                                                  at org.greenrobot.greendao.AbstractDao.queryRaw(AbstractDao.java:591)
                                                  at com.ficean.android.ficean.Windows.SingleWindow.FilesLoad(SingleWindow.java:152)
                                                  at com.ficean.android.ficean.Windows.SingleWindow.access$500(SingleWindow.java:53)
                                                  at com.ficean.android.ficean.Windows.SingleWindow$LoadFiles.doInBackground(SingleWindow.java:138)
                                                  at com.ficean.android.ficean.Windows.SingleWindow$LoadFiles.doInBackground(SingleWindow.java:130)
                                                  at android.os.AsyncTask$2.call(AsyncTask.java:292

推荐答案

您使用了错误的方法.除此之外,您还必须在查询字符串中包括columnName而不是属性本身.

You're using the wrong method. Other than that you have to include columnName to the query string not the property itself.

queryRaw()仅接受WHERE子句,如下所示:

queryRaw() accepts only the WHERE clause like this:

userDao.queryRaw("WHERE " + UserDao.Properties.Name.columnName + " = ?", "Jason")

如果您想自己编写整个select查询,则可以在下面使用此方法:

If you want to write the whole select query yourself you can use this method below:

daoSession.getDatabase().rawQuery();

哪个游标会返回给您,您应该自己处理它.

Which returns you a cursor you should handle it yourself.

但是,如果您不想在游标上苦苦挣扎,则可以使用如下代码:

But if you don't want to struggle with cursors you can use something like this:

userDao.queryBuilder().where(
    new WhereCondition.StringCondition(UserDao.Properties.Name.columnName + " IN " +
        "(SELECT DISTINCT " + UserDao.Properties.Name.columnName + " FROM " + 
        UserDao.TABLENAME + " WHERE " + UserDao.Properties.Name.columnName + " = ?",
    "Jason")
).list();

为简单起见,我以原始字符串编写以上代码:

I write the above code in raw string for the sake of simplicity:

userDao.queryBuilder().where(
    new WhereCondition.StringCondition("NAME IN (SELECT DISTINCT NAME FROM USER WHERE NAME = ?)", "Jason")
).list();

这篇关于如何在GreenDao中使用DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆