我想从表中使用按钮点击的条件检索最小余额。以下是我的代码它不工作PLZ给我解决方案。谢谢提前..
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);
}
答案 0 :(得分:1)
您只在查询中选择了MIN(余额)并尝试访问Millname
,Counttno
以及光标上的其他值,它将抛出异常。
尝试以下解决方案
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);
答案 1 :(得分:0)
以下代码完美运作......
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);
}