以下是我的sqlite查询。如何将结果保存在字符串中?我想将查询结果保存在字符串中。下面是我的查询,它在sqlite编辑器中给出了结果,但我希望在代码中得到它。
SELECT COUNT(*),SUM(Fare) FROM jobs WHERE timeweek > '2014/03/24 20:20'
private static final String KEY_COMPID = "_compid";
private static final String KEY_TIMEJOB = "timejob";
private static final String KEY_TIMEWEEK = "timeweek";
private static final String KEY_PICK = "pick";
private static final String KEY_DEST = "dest";
private static final String KEY_FARE = "fare";
+ KEY_TIME + " TEXT" + ")";
String CREATE_COMPLETED_TABLE = "CREATE TABLE " + TABLE_COMPLETED_JOBS
+ "(" + KEY_COMPID + " INTEGER PRIMARY KEY," + KEY_TIMEJOB
+ " TEXT," + KEY_PICK + " TEXT," + KEY_DEST + " TEXT,"
+ KEY_FARE + " TEXT,"
+ KEY_TIMEWEEK + " DateTime" + ")";
public String totaljobsaearnafterlogin() {
int cnt;
String selectQuery = "SELECT COUNT(*),SUM(Fare) FROM" +
TABLE_COMPLETED_JOBS + " WHERE " +KEY_TIMEWEEK+" > '2014/03/24 20:10'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
//cursor.moveToFirst();
cnt= cursor.getCount();
cursor.close();
db.close();
答案 0 :(得分:0)
String selectQuery = "SELECT COUNT(*) as count ,SUM(Fare) as fare FROM" +
TABLE_COMPLETED_JOBS + " WHERE " +KEY_TIMEWEEK+" > '2014/03/24 20:10'";
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
cnt= cursor.getCount();
if(cnt > 0) {
int count = cursor.getInt(cursor.getColumnIndex("count"));
int fareSum = cursor.getInt(cursor.getColumnIndex("fare"));
String result = count + " " + fareSum;
}
cursor.close();
答案 1 :(得分:0)
您应该迭代行并获取每个元素。
Cursor cursor = db.rawQuery(selectQuery, null);
String string = "";
if (cursor.moveToNext()) {
string += "Count: " + cursor.getString(1) + "\n";
string += "Fare: " + cursor.getString(2) + "\n";
}