当前位置: 动力学知识库 > 问答 > 编程问答 >

sqlite - Cursor on android is not returning any rows.

问题描述:

Im making an app on android and Im using an Sqlite database.

Basically, the app just inserts some numbers and then it navigates through all the rows to get the total sum of these numbers. It does that everytime the button is clicked.

But the problem is that, actually the app was working well. I just changed a little picture and then run the app again on my cell phone, but then it stop making the sum of these numbers. I used the debug option to see if the numbers were being inserted properly and the actually are. But I found out that the cursor is not getting any rows from the query. I dont know what the problem is. I really dont remmember altering the code, just changing the picture with paint. I checked the rows names, the tables names and everything is alright. I even did ctrl+z in a great desperation burst but nothing changed. I unistalled the app from my phone, clean, build and then run it again and same results.

here is the code that is involved in this operations.

public void crearTablas (SQLiteDatabase bd){

bd.execSQL("Create Table If Not Exists Gasto (ID Integer Primary Key, Descripcion Text Not Null, Costo Real Not Null, Fecha_Creado Datetime not null);");

}

public void insertarGasto(String descripcion, double costo, String fechaCreado{

ContentValues valores=new ContentValues();

valores.putNull("ID");

valores.put("Descripcion", descripcion);

valores.put("Costo", costo);

valores.put("Fecha_Creado", fechaCreado);

bd.insert("Gasto", null, valores);

}

public void crearGastoVar(String descripcion, double costo){

//redondear(double) is working well, i verified it!

bd.insertarGasto(descripcion, this.redondear(costo), fecha.get(Calendar.DATE)+"-"+(fecha.get(Calendar.MONTH)+1)+"-"+fecha.get(Calendar.YEAR));

}

public Cursor obtenerGastosVar(String fecha1, String fecha2){

return bd.query("Gasto", null, "Fecha_Creado between '"+fecha1+ "' and '"+fecha2+"'", null, null, null, null, null);

}

public double caluclarCostoGstsVar(){

Cursor c = bd.obtenerGastosVar("1-"+(fecha.get(Calendar.MONTH)+1)+"-"+fecha.get(Calendar.YEAR),

fecha.getActualMaximum(Calendar.DAY_OF_MONTH)+"-"+(fecha.get(Calendar.MONTH)+1)+"-"+fecha.get(Calendar.YEAR));

double costo=0;

if(c.moveToFirst()){ //This is returning false!!

do{

try{

Log.i("Look", "Sum is:"+costo);

costo+=Double.parseDouble(c.getString(c.getColumnIndex("Costo")));

}

catch(NumberFormatException e){

Log.i("Look", "Types are wrong");

return -2;

}

}while(c.moveToNext());

}

return redondear(costo);

}

String descrip_ = "number1";

double costo_ = 1000;

//I called before, crearTablas() so the tables are created before start inserting and reading

admin.crearGastoVar(descrip_, costo_);

admin.caluclarCostoGstsVar();

网友答案:

Try this format provided database is created properly

if (cursor != null) {

      if (cursor.moveToFirst()) {

          do {
                       //your code

              }

           while (cursor.moveToNext());

         }

  cursor.close();

Hope it helps

网友答案:

Dates/times can be stored in several formats, but if you use strings and want to compare them, you must use a format like yyyy-mm-dd (with padded fields and the most significant field first) because otherwise, string comparisons would not give correct results.

Cursors can be read with a simple while loop:

Cursor c = ...;
while (c.moveToNext()) {
    ...
}
c.close();

(Functions like query never return a null cursor, and moveToNext does the right thing for a cursor that is positioned before the first entry.)

分享给朋友:
您可能感兴趣的文章:
随机阅读: