Android 如何将从sql查询接收的数据解析为双精度
我创建了sql查询,用于从表Price中获取数据,其中我从表日志中传递了4个参数。我想检索4个价格值Android 如何将从sql查询接收的数据解析为双精度,android,sqlite,parsing,android-sqlite,Android,Sqlite,Parsing,Android Sqlite,我创建了sql查询,用于从表Price中获取数据,其中我从表日志中传递了4个参数。我想检索4个价格值我在Logs类中创建了它,我传递的参数是Logs getter。 我可能把sql查询搞砸了。 然后,我需要解析为double并使用getResult()方法进行乘法 我的问题是如何正确地创建此sql查询并将其解析为double或int? 这是我的密码。请帮忙 public double getResult() { double log_length, log_diameter, l
我在Logs类中创建了它,我传递的参数是Logs getter。
我可能把sql查询搞砸了。 然后,我需要解析为double并使用getResult()方法进行乘法
我的问题是如何正确地创建此sql查询并将其解析为double或int?
这是我的密码。请帮忙
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;
}
刚刚用新代码编辑了问题。我改变了定价方法。但这并不好。我需要得到所有4个价格,然后用getResult()方法将每个价格相乘。@RubyDigger19不要改变问题;这将使答案无效。如果这个答案解决了你原来的问题,接受它。如果你有不同的问题,问一个新的问题。好的,这很有帮助。我接受你的回答。但我能问一下如何从游标中获取解析的值并在其他方法中使用它们吗?
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();
}
}
}