我是ORMLite for android的初学者。我需要按照" name"的字母顺序从我的Accounts
表中选择某些行。列值并显示名称为&#34的行;其他"应该在列表的最后。我用queryBuilder().orderByRaw(...)
来获取它,但结果是SQLException.Below是该异常的完整堆栈跟踪。
07-22 12:06:29.730: W/System.err(581): java.sql.SQLException: Problems executing Android query: SELECT * FROM `accounts` WHERE ((`parent_id` = 1 AND `active` = 1 ) AND `id` NOT IN (5 ,43 ) ) ORDER BY CASE WHEN name=Others THEN 1 ELSE 0 END,name
07-22 12:06:29.750: W/System.err(581): at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
07-22 12:06:29.750: W/System.err(581): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:184)
07-22 12:06:29.760: W/System.err(581): at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:65)
07-22 12:06:29.771: W/System.err(581): at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
07-22 12:06:29.780: W/System.err(581): at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:247)
07-22 12:06:29.790: W/System.err(581): at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:196)
07-22 12:06:29.800: W/System.err(581): at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:265)
07-22 12:06:29.800: W/System.err(581): at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:361)
07-22 12:06:29.820: W/System.err(581): at com.j256.ormlite.stmt.Where.query(Where.java:503)
07-22 12:06:29.820: W/System.err(581): at com.fort.sample.db.SaveMoneyController.getListData(SaveMoneyController.java:183)
07-22 12:06:29.830: W/System.err(581): at com.fort.sample.view.MainActivity.loadExpense(MainActivity.java:338)
07-22 12:06:29.840: W/System.err(581): at com.fort.sample.view.MainActivity.listClicked(MainActivity.java:248)
07-22 12:06:29.860: W/System.err(581): at com.fort.sample.view.MainActivity.access$10(MainActivity.java:233)
07-22 12:06:29.860: W/System.err(581): at com.fort.sample.view.MainActivity$8.onItemClick(MainActivity.java:216)
07-22 12:06:29.860: W/System.err(581): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-22 12:06:29.899: W/System.err(581): at android.widget.ListView.performItemClick(ListView.java:3513)
07-22 12:06:29.899: W/System.err(581): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-22 12:06:29.921: W/System.err(581): at android.os.Handler.handleCallback(Handler.java:587)
07-22 12:06:29.930: W/System.err(581): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 12:06:29.940: W/System.err(581): at android.os.Looper.loop(Looper.java:123)
07-22 12:06:29.940: W/System.err(581): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-22 12:06:29.940: W/System.err(581): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 12:06:29.940: W/System.err(581): at java.lang.reflect.Method.invoke(Method.java:507)
07-22 12:06:29.940: W/System.err(581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-22 12:06:29.940: W/System.err(581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-22 12:06:29.940: W/System.err(581): at dalvik.system.NativeStart.main(Native Method)
07-22 12:06:29.979: W/System.err(581): Caused by: android.database.sqlite.SQLiteException: no such column: Others: , while compiling: SELECT * FROM `accounts` WHERE ((`parent_id` = 1 AND `active` = 1 ) AND `id` NOT IN (5 ,43 ) ) ORDER BY CASE WHEN name=Others THEN 1 ELSE 0 END,name
07-22 12:06:30.059: W/System.err(581): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-22 12:06:30.059: W/System.err(581): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-22 12:06:30.069: W/System.err(581): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-22 12:06:30.080: W/System.err(581): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
07-22 12:06:30.089: W/System.err(581): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-22 12:06:30.099: W/System.err(581): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-22 12:06:30.110: W/System.err(581): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-22 12:06:30.119: W/System.err(581): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-22 12:06:30.119: W/System.err(581): at com.j256.ormlite.android.compat.BasicApiCompatibility.rawQuery(BasicApiCompatibility.java:15)
07-22 12:06:30.129: W/System.err(581): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:180)
07-22 12:06:30.129: W/System.err(581): ... 24 more
以下是我使用的代码
listCoaAct=new ArrayList<COAAccount>();
List<Integer> idList = new ArrayList<Integer>();
idList.add(5);
idList.add(43);
List<Accounts> getCoa = dbHelper.getAccountsDao().queryBuilder()
.orderByRaw("CASE WHEN name=Others THEN 1 ELSE 0 END,name")
.where().eq("parent_id",parentId).and().eq("active", true)
.and().notIn("id", idList).query();
如何解决我的问题?
答案 0 :(得分:2)
原因就在于您的错误消息:
没有这样的专栏:其他
您必须在引号中设置“其他”,否则您的SQL引擎将尝试查找具有该名称的列。
答案 1 :(得分:1)
name=Others
是这里的问题。
在这里,Others
是一个字符串文字,所以它应该用单引号(&#39;)括起来,如:
CASE WHEN name='Others' THEN 1 ELSE 0 END,name