我想在一个函数中实现先通过一定的条件在第一张表中查询到payID的数据,然后再通过payID在另一张表中查询和payID对应的cost的值,并且把所有的cost相加起来,并返回。下面是代码public int checkMonthCost(int year, int month){
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("paytagdate", new String[]{"payID"}, "year=? and month=?",
new String[]{String.valueOf(year),String.valueOf(month)}, null, null, null);
int i = 0;
int totalcost = 0;
String[] payIDs = null;
payIDs = new String[cursor.getCount()];
while(cursor.moveToNext()){
String payID = cursor.getString(cursor.getColumnIndex("payID"));
payIDs[i] =payID;
i++;
}
cursor.close();
Cursor cursor1= db.query("pay", new String[]{"cost"}, "payID=?", payIDs, null, null, null);
while(cursor1.moveToNext()){
totalcost += Integer.parseInt(cursor1.getString(cursor1.getColumnIndex("cost")));
}
cursor1.close();
return totalcost;
}这是两个数据表
db.execSQL("CREATE TABLE IF NOT EXISTS pay(payID integer primary key autoincrement,use text,cost text)"); db.execSQL("CREATE TABLE IF NOT EXISTS paytagdate(ptagID integer primary key autoincrement,year integer,month integer,day integer,payID integer)");
在eclipse中报错说cannot bind argument at index 3 我觉得是Cursor cursor1= db.query("pay", new String[]{"cost"}, "payID=?", payIDs, null, null, null);出错了 但是应该怎么修改啊,求大神帮助
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("paytagdate", new String[]{"payID"}, "year=? and month=?",
new String[]{String.valueOf(year),String.valueOf(month)}, null, null, null);
int i = 0;
int totalcost = 0;
String[] payIDs = null;
payIDs = new String[cursor.getCount()];
while(cursor.moveToNext()){
String payID = cursor.getString(cursor.getColumnIndex("payID"));
payIDs[i] =payID;
i++;
}
cursor.close();
Cursor cursor1= db.query("pay", new String[]{"cost"}, "payID=?", payIDs, null, null, null);
while(cursor1.moveToNext()){
totalcost += Integer.parseInt(cursor1.getString(cursor1.getColumnIndex("cost")));
}
cursor1.close();
return totalcost;
}这是两个数据表
db.execSQL("CREATE TABLE IF NOT EXISTS pay(payID integer primary key autoincrement,use text,cost text)"); db.execSQL("CREATE TABLE IF NOT EXISTS paytagdate(ptagID integer primary key autoincrement,year integer,month integer,day integer,payID integer)");
在eclipse中报错说cannot bind argument at index 3 我觉得是Cursor cursor1= db.query("pay", new String[]{"cost"}, "payID=?", payIDs, null, null, null);出错了 但是应该怎么修改啊,求大神帮助