2012-08-01 53 views
1

在我的应用程序中,我显示的数据来自数据库的表视图。我的要求是,从数据库我必须检索将在当月落入的数据。我写了查询,但它来了为0.实际上我今天的日期在数据库中有1项,所以我的查询应该返回该数据,但它显示为0.请帮助我。提前感谢。在android中的数据库检索

我的查询如下:

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; 
} 

我得到的是总,并在表的布局atextview显示..

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

你检查了cursor1.getCount()吗? – 2012-08-01 04:56:45

+0

没有尼克..我会.. – 2012-08-01 04:57:19

+0

S尼克...它是作为1 .. – 2012-08-01 04:59:39

回答

0

最大的可能是没有错的查询,但你传递的方式将查询结果传递给ListView。你能证明你是如何做到的吗?也许我可以帮忙。

或者你可以看看herehere


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/

+0

对不起halim..not listview它的表格布局.. .sorry我犯了一个错误在我的问题..反正我会发布我的代码,.. – 2012-08-01 04:56:58

+0

好吧首先确保什么值的字符串housetotal返回1预期。 TableLayout也应该能够显示来自db的值。 – rxlky 2012-08-01 05:01:45

+0

Halim ... housetotal应该返回总金额,并且我必须在textview中显示.. – 2012-08-01 05:05:29

0

你为什么不通过在query..might给你的表的列名尝试制定出you..specify要以检索列..

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; 
} 

尝试此方法....

相关问题