数据库内连接QUERY游标问题SQLite

时间:2015-04-11 11:26:48

标签: android sqlite

表用户: 用户ID,用户名,密码,年龄,电子邮件,性别,身高,体重,bmi,bmr

表卡路里摄入量: userid,(DATE)intakedate,intakeday,calories;

我试图从表用户INNER JOIN表Cal In ON用户中选择卡路里。 uid == Cal.userid,其中userid =?和当前日期=?

以下是查询:

public CalorieIntake getuserIntake(int id, Date currentdate) {
    CalorieIntake calorieIntake = null;
    SQLiteDatabase db = getReadableDatabase();
    final String UserintQuery = "SELECT " + KEY_CALORIES + " FROM "
            + TABLE_CALORIE_INTAKE + " c " + " INNER JOIN " + TABLE_USERS + " u " +
            "ON" + " c." + KEY_USER_ID + " = u." + KEY_U_ID + " WHERE u." + KEY_U_ID + " =? AND " + KEY_INTAKE_DATE + " =?";
    Cursor cursor;
    cursor = db.rawQuery(UserintQuery, new String[]{String.valueOf(id), currentdate});

    if (cursor.getCount() < 0) {
        cursor.moveToFirst();
        assert cursor != null;
        calorieIntake = new CalorieIntake(cursor.getInt(0),cursor.getDate??,
                cursor.getString(2),
                cursor.getDouble(3));


    }
    cursor.close();
    return calorieIntake;
}
我把两个?在cursor.getDate旁边突出显示它,因为我无法找到检索类型日期的方法。

1 个答案:

答案 0 :(得分:-1)

使用此格式将日期数据存储在文本中 - &gt; “MM / dd / yyyy HH:mm:ss”

然后使用此方法:

public static Date getDate(Cursor cursor, String colName)
            throws ParseException {
        int idx = cursor.getColumnIndex(colName);

        if (!cursor.isNull(idx)) {
            String strDate = cursor.getString(idx);
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "MM/dd/yyyy HH:mm:ss");
            Date convertedDate = dateFormat.parse(strDate);

            return convertedDate;

        } else
            return null;
    }