如何将从sql查询中获取的数据解析为double

时间:2016-03-08 22:43:03

标签: android sqlite parsing android-sqlite

我创建了sql查询,用于从表格中获取数据,其中我从表格Logs传递了4个参数。我想检索4个价格值。我在我的Logs类中创建了这个,我传递的参数是Logs getters 我可能搞砸了sql查询。 之后,我需要使用方法getResult()解析为double和multiply。

我的问题是如何正确创建这个SQL查询并将其解析为double或int?
这是我的代码。请帮忙。

{{1}}

1 个答案:

答案 0 :(得分:-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();
        }
    }


}