android:如何在sqlite中将varchar或integer类型的值转换为real(float)类型

时间:2011-09-20 09:42:32

标签: android sqlite casting

我有两列。当我划分两个表的值时,我得到整数值的列。但我希望除法后列中的确切浮点值。 我有一个问题是,当我在shell中运行此查询时它工作正常但是当我在代码中使用它时它给了我错误。请提出一些建议。

public ArrayList<String> selectQueryForcasting(Context context,String segment,String customer, String location)
        {   
            ArrayList<String> z = new ArrayList<String>();
            Log.d("Inside Select ","Select starts for table name: Sales_Forcast");
            myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_READABLE, null);  
                            System.out.println("inside");
                                Cursor c= myDataBase.rawQuery("select ((Forecasted_Qty - Actual_Sales_Qty) * 100)/forecasted_qty from sales_forecast,customer,market_segment,org_location where market_segment.Market_Seg_Code="+segment+" and sales_forecast.Mrkt_Segment_Key=market_segment.Market_Seg_Key group by sales_forecast.time_key", null);
                System.out.println("Succcess"+c);

                if (c.moveToFirst()) {
                    do {

                        z.add(c.getString(0));
                        //Log.d("EXAMPLE","string ");

                    } while (c.moveToNext());
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }


            return z;
        } 

2 个答案:

答案 0 :(得分:2)

这可以让你得到你想要的东西:

选择((强制转换(Forecasted_Qty为REAL) - 强制转换(Actual_Sales_Qty为REAL))* 100)/ forecasted_qty

或更好:

选择((强制转换(Forecasted_Qty - Actual_Sales_Qty为REAL))* 100)/ forecasted_qty

答案 1 :(得分:0)

我得到了答案。

c= myDataBase.rawQuery("select ((cast(Forecasted_Qty - Actual_Sales_Qty as real)) * 100)/forecasted_qty,Mrkt_Segment_Key from sales_forecast,customer,market_segment,org_location  where sales_forecast.Mrkt_Segment_Key=market_segment.market_seg_key and market_segment.market_seg_code=? group by sales_forecast.time_key",new String[]{"+segment+" });