Moghu
Moghu

Reputation: 1

App Development using eclipse

I want to retrieve minimum balance from table using where condition on button click.Following is my code it's not working plz give me solution.Thanks in advance..

smname = spn_mname.getSelectedItem().toString().trim();

yname = spn_yname.getSelectedItem().toString().trim();

scno = spn_count.getSelectedItem().toString().trim();

String dbsyname = null, dbsmname = null, dbsycolor = null, dbscount = null, dbsrqty = null, dbsdqty = null, dbsbaln = null;
DataBaseHelper dbh = new DataBaseHelper(StockActivity.this);

SQLiteDatabase db = dbh.getReadableDatabase();

Cursor cursor = db.rawQuery("SELECT MIN(Balance) FROM stocktable",null);
if (cursor.moveToFirst()) {
    do {
        dbsmname = cursor.getString(cursor.getColumnIndex("Millname"));
        dbsyname = cursor.getString(cursor.getColumnIndex("Yarnname"));
        dbscount = cursor.getString(cursor.getColumnIndex("Counttno"));

        if (dbsmname.equals(smname) 
                && bsyname.equals(syname)
                && dbscount.equals(scno)) {
            dbsbaln = cursor.getString(cursor.getColumnIndex("Balance"));
        }

    } while (cursor.moveToNext());

    edtrqty.setText(dbsbaln);
}

Upvotes: 0

Views: 24

Answers (2)

Moghu
Moghu

Reputation: 1

Following code works perfectly...

Cursor cursor = db.rawQuery("SELECT MIN(Balance) AS balance FROM " + DataBaseHelper.stocktable + " WHERE Millname = ? AND Yarnname = ? AND Counttno = ? ", new String[] { smname, syname, scno });

            if (cursor.moveToFirst()) {
                do {
                    dbsbaln = cursor.getString(cursor
                            .getColumnIndex("balance"));

                } while (cursor.moveToNext());
                edtrqty.setText(dbsbaln);
            }

Upvotes: 0

MJM
MJM

Reputation: 5311

you have only selected MIN(Balance) only in your query and trying to access Millname,Counttno and other value on cursor it will throw the exception.

Try Below Solution

    Cursor cursor = db.rawQuery("SELECT MIN(Balance) balance as  FROM stocktable 
 where Millname=? AND bsyname=? AND dbscount=?",new String[]{smname,syname,scno});
  if (cursor.moveToFirst()) {
      do {
            dbsbaln = cursor.getString(cursor.getColumnIndex("balance"));
          }

      } while (cursor.moveToNext());
        edtrqty.setText(dbsbaln);

Upvotes: 1

Related Questions