2011-09-20 2 views
0

두 개의 컬럼이 있습니다. 내가 두 테이블의 값을 나눌 때 나는 정수 값의 열을 얻고있다. 그러나 나누기 후 열의 정확한 float 값을 원합니다. 내가 가진 또 하나의 쿼리는 쉘에서이 쿼리를 실행할 때 잘 동작하지만 코드에서 사용할 때 오류가 발생합니다. 제발 좀 제안 해주세요.android : sqlite에서 varchar 또는 정수 유형의 값을 실수 (실수) 유형으로 변환하는 방법

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; 
     } 
+0

답례로 문자열을 받고 있습니까? Float.parseFloat ("String here");를 사용할 수 있습니다. – doNotCheckMyBlog

답변

2

이 당신이 원하는 걸 얻을해야합니다

선택 ((캐스트 (Forecasted_Qty REAL) 등 -) REAL로 캐스트 (Actual_Sales_Qty) * 100)/forecasted_qty

또는 더 나은 아직 :

선택 ((캐스트 (Forecasted_Qty - Actual_Sales_Qty as REAL)) * 100)/forecasted_qty

+0

감사합니다. 제발 질문에 문제를 말해주십시오. 그것은 나를 오류를 보여줍니다 – neha

+0

고마워 잘 됐어. 제발 질문에 문제를 말해주십시오. 그것은 나에게 오류를 보이고있다. myDataBase.rawQuery (.. query ..) 위 코드에서 작성한대로. 내가 "어디서"부분을 제거하면 잘 작동합니다. where 절의 구문이 올바르지 않습니다. 다른 대안이 있다면 제안하십시오. – neha

+0

@neha 세그먼트 변수에 숫자가 아닌 데이터가 들어 있으면 작은 따옴표로 묶어야합니다. 그렇지 않으면 sqlite가 다른 열과 비교하려고한다고 생각합니다. 시도 : market_segment.Market_Seg_Code = ' "+ 세그먼트 +"'및 ... –

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+" }); 
관련 문제