2016-03-08 90 views
0

我創建了SQL查詢從表中獲取數據價格,其中我從表日誌傳遞4個參數。我想檢索4個價格值。
我在我的Logs類中創建了這個,我傳遞的參數是Logs getters。
我可能搞砸了sql查詢。 之後,我需要解析成double和multiply方法getResult()。

我的問題是如何正確地創建此sql查詢並解析它爲double或int?
這是我的代碼。請幫忙。如何解析從sql查詢收到的數據到雙重

public double getResult() { 
     double log_length, log_diameter, length, diameter, result; 
     log_length = Integer.parseInt(getLength().toString()); 
     log_diameter = Integer.parseInt(getDiameter().toString()); 
     length = log_length/100; 
     diameter = log_diameter * log_diameter * 3.14159265; 
     result = length * diameter/40000; 
     return result; 
    } 

    public Cursor getPrice() { 
     Cursor cursor = db.query("Price", new String[]{"price_stump_kn", "price_stump_eur", "road_price_kn", "road_price_eur"}, "sort = ? AND grade = ? AND length = ? BETWEEN diameter_dg = ? AND diameter_gg = ?", 
       new String[]{getSort_id(), getGrade(), getLength(), getDiameter(), getDiameter()}, null, null, null); 
     if (cursor.moveToFirst()) { 
      do { 
       Price price = new Price(); 
       price.setStumpPrice_kn(cursor.getString(0)); 
       price.setStumpPrice_eur(cursor.getString(1)); 
       price.setRoadPrice_kn(cursor.getString(2)); 
       price.setRoadPrice_eur(cursor.getString(3)); 
      } while (cursor.moveToNext()); 
     } 
     return cursor; 
    } 

回答

-1
public class MainActivity extends AppCompatActivity { 


    public static SQLiteDatabase simpledb=null; 
    public static String dbname="demo"; 
    public static String tablename="price"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     simpledb=this.openOrCreateDatabase(dbname, MODE_PRIVATE, null); 

     try 
     { 

      simpledb.execSQL("CREATE TABLE IF NOT EXISTS " + tablename + "(id INTEGER PRIMARY KEY AUTOINCREMENT,price1 double,price2 double,address varchar)"); 
      simpledb.execSQL("insert into '" + tablename + "'(price1,price2,address)values('2201.28','802.78','xyz');"); 
      simpledb.execSQL("insert into '" + tablename + "'(price1,price2,address)values('222656','707.77','xyz');"); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    Fildata(); 
} 

    public void Fildata() 
    { 
     try 
     { 
      Cursor c=simpledb.rawQuery("select * from '"+tablename+"')",null); 

      System.out.print("result"+c.toString()); 
      Log.d("SQL ", c.toString()); 
      if(c.getCount()>0) 
      { 
       while (c.moveToNext()) 
       { 
        double price1=c.getDouble(1); 
        double price2=c.getDouble(2); 
        add=c.getString(3); 

        Toast.makeText(getApplicationContext()," "+price1+" "+price2,Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 


} 
+0

剛纔編輯與新代碼的問題。我改變我的價格方法。但它不好。我需要獲得所有4個價格,並用getResult()方法將每個價格乘以。 – RubyDigger19

+0

@ RubyDigger19不要改變問題;這會使答案無效。如果此答案解決了您的原始問題,請接受它。如果您有其他問題,請提出一個新問題。 –

+0

好的,它有幫助。我會接受你的回答。但我可以問你如何從遊標獲取解析值並在其他方法中使用它們? – RubyDigger19