在android中尝试在sqlite查询中求和时,列不存在异常

时间:2014-06-25 10:28:40

标签: android sqlite

我使用SQlite数据库存储数据..但是当我尝试使用查询对列进行求和时,我收到错误Column doesn't exists,如下所示:

select _id, column1, sum(column2),sum(column3) from table

在我没有求和的同时,我能够在应用程序中看到结果,如下所示。

select _id, column1, column2,column3 from table

Column2和Column3是Real数据类型列。

我对这种行为一无所知..为什么这样做......

专家可以建议我出路。

感谢您的时间。 代码tp从游标中恢复列。

summaryviewholder.Total.setText(String.valueOf(getsummary.getInt(getsummary.getColumnIndexOrThrow(st.column2))));

logcat的:

E/AndroidRuntime(8187): FATAL EXCEPTION: main
06-25 15:53:24.011: E/AndroidRuntime(8187): Process: com.example.portfoliomanager, PID: 8187
06-25 15:53:24.011: E/AndroidRuntime(8187): java.lang.IllegalArgumentException: column 'Buy_Qua' does not exist
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.example.StockDirector.stockmanager$popsummarystock.bindView(stockmanager.java:518)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.example.StockDirector.stockmanager$popsummarystock.newView(stockmanager.java:539)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.AbsListView.obtainView(AbsListView.java:2720)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.ListView.onMeasure(ListView.java:1186)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5352)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.View.measure(View.java:17387)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2214)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1351)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1550)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1235)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6476)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.Choreographer.doCallbacks(Choreographer.java:603)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.Choreographer.doFrame(Choreographer.java:573)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.os.Handler.handleCallback(Handler.java:733)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.os.Looper.loop(Looper.java:157)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at android.app.ActivityThread.main(ActivityThread.java:5356)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at java.lang.reflect.Method.invoke(Method.java:515)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-25 15:53:24.011: E/AndroidRuntime(8187):     at dalvik.system.NativeStart.main(Native Method)
06-25 15:53:25.486: I/Process(8187): Sending signal. PID: 8187 SIG: 9

2 个答案:

答案 0 :(得分:2)

您必须为您汇总的行命名。

其他说明:

如果您使用sum(或一般的聚合功能),则无法选择未汇总的其他行,否则您将无法获得正确的结果。 此外,使用Float从数据库中获取值,因为在sqlite中使用Real数据类型(浮点数)。

所以不要这样:

select _id, column1, sum(column2),sum(column3) from table

你必须做这样的事情:

select column1, sum(column2) AS sum1, sum(column3) AS sum2 from table group by column1;

以及价值观:

summaryviewholder.Total.setText(String.valueOf(getsummary.getFloat(getsummary.getColumnIndexOrThrow("sum1"))));

答案 1 :(得分:1)

当您编写SELECT sum(Column2)时,输出列的名称为sum(Column2)

您应该按索引读取列值(当查询发生更改时这很危险),或者给输出列另一个名称:

SELECT sum(Column2) AS Column2Sum ...