所以我不确定我在这里做错了什么,但是我希望从这个SQLite语句中获取值并将其分配给一个值,然后我可以将其放入DB中)
String mpg = new String("select (" + gasLog.getOdometer() + " - y.odometer) / " + gasLog.getGallons() + " as mpg from tbl x, tbl y where y.odometer = (select max(z.odometer) from tbl z where z.odometer < " + gasLog.getOdometer() + ")");
values.put(KEY_MPG, mpg);
但是,每当我运行它时,它只是将实际语句放入数据库中的单元格...所以它插入“select(”+ ...“
有人可以帮助我解决我在这里做错了什么,为了将数据从这里拿到数据库而不是语句本身,我需要做些什么?
谢谢!
答案 0 :(得分:0)
我这样做了,谢谢你的提示!
Cursor cursor = getWritableDatabase().rawQuery("select (" + gasLog.getOdometer() + " - y.odometer) / " + gasLog.getGallons() + " as mpg from gasLog x, gasLog y where y.odometer = (select max(z.odometer) from gasLog z where z.odometer < " + gasLog.getOdometer() + ")", null);
try {
if (cursor.moveToFirst()) {
mpg = cursor.getString(0);
}
} finally {
cursor.close();
}
values.put(KEY_MPG, mpg);