简体   繁体   English

无法弄清楚为什么SQL语句不能正常运行

[英]Can't figure out why SQL statement isn't woking

I can't understand what's wrong with my statement. 我不明白我的陈述有什么问题。 Seems right to me, and the cursor should include both rows in my database table. 对我来说似乎正确,并且光标应在数据库表中同时包含这两行。 According to SQLite website this formatted date is supported (Also tried with other supported dates). 根据SQLite网站的说法,此格式的日期受支持(也尝试使用其他受支持的日期)。

        String statement = "SELECT * FROM " + TABLE_DISTANCE_NAME + " WHERE " + COLUMN_TIME_DISTANCE_ADDED + " BETWEEN " + "2014-07-14" + " AND " + "2014-08-14";
        Log.d("statement", statement);
        Cursor cur = db.rawQuery(statement, null);
        Log.d("cursor amount rows", Integer.toString(cur.getCount()));

        if (cur != null) {
            cur.moveToFirst();
        }    
        int totalDistance = 0;
        while (cur.isAfterLast() == false) {
            Log.d("cursor distance value", Integer.toString(cur.getInt(0)));
            totalDistance += cur.getInt(0);
            cur.moveToNext();
        }
        Log.d("Total Distance traveled = ", Integer.toString(totalDistance));

This is what the table looks like. 这就是表格的样子。 这是我的桌子的样子

Log: 日志:

...statement﹕ SELECT * FROM distanceTraveled WHERE timedistadded BETWEEN 2014-07-14 AND 2014-08-14

com.example.RoadTrip D/cursor amount rows﹕ 0
com.example.RoadTrip D/Total Distance traveled =﹕ 0

Thank you 谢谢

Date constants need to be enclosed in single quotes: 日期常量需要用单引号引起来:

" WHERE " + COLUMN_TIME_DISTANCE_ADDED + " BETWEEN " + "'2014-07-14'" + " AND " + "'2014-08-14'";

This problem would be more readily apparent if you printed out the SQL statement after the substitution. 如果在替换后打印出SQL语句,此问题将更容易显现。 That is always a good first step in trying to debug this type of problem. 这始终是尝试调试此类问题的良好第一步。

try quoting you query paramaters 尝试引用您查询的参数

eg 例如

+ "'2014-07-14'" + " AND " + "'2014-08-14'"

Dates and Strings need to be single quoted as per SQL. 根据SQL,日期和字符串需要用单引号引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM