1

私のアプリケーションでは、データベースからのデータをテーブルビューで表示しています.私の要件は、データベースから今月のデータを取得する必要があることです.クエリを書きましたが、0.実際には1です.今日の日付でデータベースにエントリがあるため、クエリはそのデータを返すはずですが、0 と表示されています。助けてください。よろしくお願いします。

私のクエリは次のとおりです。

public String addgroupincome(String grp) throws SQLException
{   
    long sum=0; 
    Cursor cursor1 = db.rawQuery(
             "SELECT SUM("+(KEY_TOTAL)+") FROM incomexpense WHERE date= Strftime('%Y-%m','now') AND category='Income' AND groups='"+grp+"'",null);
     if(cursor1.moveToFirst())
     {
       sum = cursor1.getLong(0);
     }
     cursor1.close();   
     String housetotal=String.valueOf((long)sum);    
     return housetotal; 
}

私はその合計を取得し、テーブルレイアウトのテキストビューに表示しています..

final  String houtotal=db.addgroupincome(group1);  
     housetotal.setText(houtotal);
4

3 に答える 3

0

クエリでテーブルの列名を指定してみませんか..うまくいくかもしれません..取得する列を指定してください..

于 2012-08-01T05:48:31.010 に答える
0
if (cursor.moveToNext()) {
  cursor.moveToFirst();
  while (cursor.isAfterLast() == false) {
    Log.i(ID, cursor.getInt(0) + "");
    cursor.moveToNext();
  }
  cursor.close();
} else {
  cursor.close();
  return null;
}

この方法を試してください....

于 2012-08-01T06:00:56.090 に答える
0

ほとんどの場合、クエリに問題はありませんが、クエリ結果を ListView に渡す方法に問題はありません。あなたはそれを行う方法を示すことができますか?おそらく私は助けることができました。

または、こちらまたはこちらをご覧ください


public int getCount() {
    DBHelper dbHelper = DBHelper.getDBAdapterInstance(this);
    int count = 0;

    try {
        dbHelper.openDataBase();

        String query = "select count(1) from t_model where upper(brandName) = upper('"
                + selectedBrand + "') order by modelName ASC";

        Cursor cursor = dbHelper.selectRecordsCursor(query, null);

        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }

        cursor.close();
        cursor = null;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        dbHelper.close();
    }

    return count;
}

TextView の場合は、次のように単純にする必要があります

TextView tvCount = (TextView) findViewById(R.id.tvCount);
tvCount.setText("Count : " + getCount);

クエリのデバッグに問題がある場合。http://sqlitebrowser.sourceforge.net/またはhttp://www.sqliteexpert.com/を試してください

于 2012-08-01T04:55:00.393 に答える