我正在查询数据库以获取输入的最近24小时内的值的光标。我可以查询并获取我想要的A_VALUE列的所有值,但我无法弄清楚如何或在何处正确编写子查询以获取A_VALUE列与where子句参数的总和并获取其值。任何想法将不胜感激。 (没有编码背景,我正在学习本网站和其他网站的所有内容)
//过去24小时内获取值的当前代码是:
String[] projection = {
DrinkContract.DrinkEntry._ID,
DrinkContract.DrinkEntry.COLUMN_A_VALUE,
DrinkContract.DrinkEntry.COLUMN_TIME_DATE};
//code to query databse for last 24 hours
String selection = DrinkContract.DrinkEntry.COLUMN_TIME_DATE + " BETWEEN strftime('%s', 'now', '-1 day') AND strftime('%s','now') ";
Cursor dayCursor = getContentResolver().query(
DrinkContract.DrinkEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
selection, // selection clause
null,
DrinkContract.DrinkEntry.COLUMN_TIME_DATE); // Default sort order*/
if (dayCursor == null || dayCursor.getCount() < 1) {
return;
}
// Proceed with moving to the first row of the cursor and reading data from it
while (dayCursor.moveToNext()) {
int aValueColumnIndex = dayCursor.getColumnIndex(DrinkContract.DrinkEntry.COLUMN_A_VALUE);
int drinkTimeDateColumnIndex = dayCursor.getColumnIndex(DrinkContract.DrinkEntry.COLUMN_TIME_DATE);
// Extract out the value from the Cursor for the given column index
double aValue = dayCursor.getDouble(aValueColumnIndex);
System.out.println("The total a value is " + aValue);
答案 0 :(得分:0)
//工作代码
String[] projection = {
DrinkContract.DrinkEntry._ID,
DrinkContract.DrinkEntry.COLUMN_TIME_DATE,
"total("+DrinkContract.DrinkEntry.COLUMN_A_VALUE+") AS totalA "};
//code to query databse for last 24 hours
String selection = DrinkContract.DrinkEntry.COLUMN_TIME_DATE + " BETWEEN strftime('%s', 'now', '-1 day') AND strftime('%s','now') ";
Cursor dayCursor = getContentResolver().query(
DrinkContract.DrinkEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
selection, // selection clause
null,
DrinkContract.DrinkEntry.COLUMN_TIME_DATE); // Default sort order*/
if (dayCursor == null || dayCursor.getCount() < 1) {
return;
}
// Proceed with moving to the first row of the cursor and reading data from it
while (dayCursor.moveToNext()) {
int aValueColumnIndex = dayCursor.getColumnIndex("totalA");
int drinkTimeDateColumnIndex = dayCursor.getColumnIndex(DrinkContract.DrinkEntry.COLUMN_TIME_DATE);
// Extract out the value from the Cursor for the given column index
double aValue = dayCursor.getDouble(aValueColumnIndex);
System.out.println("The total a value is " + aValue);