0

我还有一个问题 DBAdapter.java

public Cursor getTotalFlightTime_Custom() throws SQLException{
return db.rawQuery("SELECT time(sum(strftime('%s', Total_Flight_Time) - strftime('%s','00:00:00')),'unixepoch') FROM Flights WHERE Date BETWEEN '01/01/2012' AND '01/02/2012'",null);
}

MainActivity.java

private void CustomReport(){
        DBHelper.open();

        TextView txt = (TextView)findViewById(R.id.lblReportTotalFlightTimeAUTO);
        Cursor cursor = DBHelper.getTotalFlightTime_Custom();
        cursor.moveToFirst();
        String[] names = cursor.getColumnNames();
        String AUTO = cursor.getString(cursor.getColumnIndex(names[0]));
    if(AUTO == "")
        txt.setText("00:00:00");
    else
        txt.setText(AUTO);

}

问题是查询不起作用。我希望查询从Total_Flight_Time列返回SUM,表航班采用 HH:MM:SS 格式,用于日期列在01/01/201201/02/2012 (DD/MM/YYYY) 之间的行。我也尝试过 YYYY-MM-DD 格式,但程序只是崩溃而没有消息。

我想我知道问题出在哪里,但我不确定:

WHERE Date BETWEEN '01/01/2012' AND '01/02/2012'

但我不知道如何解决它。查询的另一部分应该是正确的,因为我将它用于同一列但整个表,它按我预期的那样工作。

EIDT:数据列存储为INTEGER

你能帮助我吗?感谢您的建议:D

评论代码:

String dateStringfrom = "01/01/2012";
            String dateStringto = "01/02/2012";
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date convertedDatefrom = new Date();
            Date convertedDateto = new Date();
            try {
                convertedDatefrom = dateFormat.parse(dateStringfrom);
                convertedDateto = dateFormat.parse(dateStringto);

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(convertedDatefrom);
            System.out.println(convertedDateto);
            Log.d("Converted From","result: " + convertedDatefrom);
            Log.d("Converted To","result: " + convertedDateto);
4

1 回答 1

1

DateTime列应该是numeric而不是integerSQLite。

http://www.sqlite.org/datatype3.html

您必须将字符串转换为日期才能使其在条件下工作。

SELECT * FROM Table WHERE DateField BETWEEN Date('01/01/2012') AND Date('01/02/2012')

编辑:不确定这是否会有所帮助,但请尝试一下。

SELECT strftime('%s',Total_Flight_Time)-strftime('%s','00:00:00') AS TimeDelta, SUM(TimeDelta) AS TimeSum, time(TimeSum,'unixepoch') AS FlightTime FROM Flights WHERE Date BETWEEN Date('01/01/2012') AND Date('01/02/2012')

于 2013-07-14T22:19:13.363 回答