我有以下方法。
当我运行它时,列“D”返回空值但是如果我在sqllite管理器中运行打印的sql字符串,则有些值虽然有些行为空。
这是方法。
public List<ActiveTenancy> getUnInvoicedRooms(String mydate)
{
List<ActiveTenancy> payments= new ArrayList<ActiveTenancy>();
String sql ="SELECT Apartments.Number as N,Tenants.FirstName as F,Tenants.SurName as S ,"+
" (select Max(Rental_invoices.enddate) from Rental_invoices inner Join Tenancy on Tenancy._id=Rental_invoices.tenancy_id "+
" where Tenancy.tenant_id=Tenants._id ) as D" +
" from Tenants "+
" inner join Tenancy on Tenancy.tenant_id=Tenants._id "+
" inner join Apartments on Apartments._id=Tenancy.apartment_id "+
" where Tenancy._id not in(select Rental_invoices.tenancy_id from Rental_invoices where enddate " + ">=?" + ")"+
" and Tenancy.InActive=0";
String[] args= new String[]{mydate};
Log.e("sql", sql);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(sql, args);
if(c.moveToFirst()){
do{
ActiveTenancy a= new ActiveTenancy();
a.setFirstName(c.getString(c.getColumnIndex("F")));
a.setSurname(c.getString(c.getColumnIndex("S")));
a.setApartment(c.getString(c.getColumnIndex("N")));
if(c.getString(c.getColumnIndex("D"))!=null)
{
Log.e("dddd",c.getString(c.getColumnIndex("D")));
try {
a.setEnd_date(fm.parse(c.getString(c.getColumnIndex("D"))));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
payments .add(a);
} while(c.moveToNext());
}
c.close();
return payments;
}
造成这种情况的原因是什么?
罗纳德
不使用参数持有人“?”即将参数直接传递给sql语句,它现在正在工作。
String sql ="SELECT Apartments.Number as N,Tenants.FirstName as F,Tenants.SurName as S ,"+
" (select Max(Rental_invoices.enddate) from Rental_invoices inner Join Tenancy on Tenancy._id=Rental_invoices.tenancy_id "+
" where Tenancy.tenant_id=Tenants._id ) as D" +
" from Tenants "+
" inner join Tenancy on Tenancy.tenant_id=Tenants._id "+
" inner join Apartments on Apartments._id=Tenancy.apartment_id "+
" where Tenancy._id not in(select Rental_invoices.tenancy_id from Rental_invoices where enddate " + ">='" + mydate + "'"+
")"+
" and Tenancy.InActive=0";
任何解释?....