游标查询返回0

时间:2017-04-24 04:23:39

标签: java android android-database

我正在使用数据库构建一个应用程序,但是当我从数据库中检索它返回空值(即返回的游标有0个元组)时会出现问题,我确信数据库中包含一些信息。 如果您发现任何错误,我正在提供我的代码,请提供帮助。

用户将数据输入数据库的片段:

public Cursor getCarEntry(String username, String Day, String Type) {

    Cursor carCursor = db.query(true, "CAR", new String[]{"distance, time"}, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
    if (carCursor.getCount() < 1) {
        carCursor.close();
    }
        carCursor.moveToFirst();
        for (int i = 0; i < carCursor.getCount(); i++) {
            int distance = carCursor.getInt(carCursor.getColumnIndex("DISTANCE"));
            int time = carCursor.getInt(carCursor.getColumnIndex("TIME"));
        }
        return carCursor;

这是我的疑问:

            carStandardCursors[i] = carDataBaseAdapter.getCarEntry(currentUsername, searchDate, "Standard");

然后我在片段

中检索它
        for (int i = 0; i < 7; i++) {
        Cursor ccursor = carStandardCursors[i];
        carStandardTotal = CalculateCarStandard(ccursor);
        carStandard[dex] = carStandardTotal;

然后我做了一个循环来对游标进行数学运算

n=(No Two "" All)
3a(){ echo "${n[$((($1==$2)+($1==$3)+($3==$2)))]} numbers are equal." ; }

我发现carStandardCursors [i]中的值都是零。

2 个答案:

答案 0 :(得分:0)

使用Facebook Stetho Library检查数据库是否包含值。然后,只有您可以了解。

stetho library

答案 1 :(得分:0)

问题出在这里,

SELECT DATE_FORMAT(`date`, '%d-%m-%Y') as date_formatted
FROM locate 
ORDER BY `datetime` DESC

将其更改为

    public Cursor getCarEntry(String username, String Day, String Type) {

Cursor carCursor = db.query(true, "CAR", **new String[]{"distance, time"}**, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
if (carCursor.getCount() < 1) {
    carCursor.close();
}
    carCursor.moveToFirst();
    for (int i = 0; i < carCursor.getCount(); i++) {
        int distance = carCursor.getInt(carCursor.getColumnIndex("DISTANCE"));
        int time = carCursor.getInt(carCursor.getColumnIndex("TIME"));
    }
    return carCursor;

然后将光标放在方法调用上,然后在

   public Cursor getCarEntry(String username, String Day, String Type) {

Cursor carCursor = db.query(true, "CAR", new String[]{"DISTANCE", "TIME"}, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
if (carCursor.getCount() < 1) {
    return carCursor;
}else{
     if (carCursor!= null) {
        carCursor.moveToNext();
    }

    return carCursor;
}

如果有多个数据,则使用while循环和ArrayAdapter