Error fetching data

0

I have an application that should enter data on one screen and the other query on the database

The problem is that whenever I try to run the error query:

Log

11-16 18:43:31.023: I/ActivityManager(26887): Timeline: Activity_launch_request     id:br.com.exercicio8 time:58351239
11-16 18:43:31.103: D/AndroidRuntime(26887): Shutting down VM
11-16 18:43:31.103: W/dalvikvm(26887): threadid=1: thread exiting with uncaught    exception (group=0x41996d88)
11-16 18:43:31.103: E/AndroidRuntime(26887): FATAL EXCEPTION: main
11-16 18:43:31.103: E/AndroidRuntime(26887): Process: br.com.exercicio8, PID: 26887
11-16 18:43:31.103: E/AndroidRuntime(26887): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.exercicio8/br.com.exercicio8.LocalizacaoAtual}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.os.Looper.loop(Looper.java:212)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread.main(ActivityThread.java:5135)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at java.lang.reflect.Method.invoke(Method.java:515)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at dalvik.system.NativeStart.main(Native Method)
11-16 18:43:31.103: E/AndroidRuntime(26887): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at br.com.exercicio8.LocalizacaoAtual.onLocationChanged(LocalizacaoAtual.java:85)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at br.com.exercicio8.LocalizacaoAtual.onCreate(LocalizacaoAtual.java:46)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.Activity.performCreate(Activity.java:5231)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 18:43:31.103: E/AndroidRuntime(26887):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
11-16 18:43:31.103: E/AndroidRuntime(26887):    ... 11 more

Code that runs SQLiteOpenHelper

public Cursor consult(int latiude, int longitude){
          SQLiteDatabase db = this.getReadableDatabase();
            String query = "select "+CADASTRO_COLUMN_NOMELOCAL+" from " + LOCATION_TABLE_NAME + " where + " + CADASTRO_COLUMN_LATITUDE + " = " + latiude + " AND "+ CADASTRO_COLUMN_LONGITUDE + " = " +longitude;
            Cursor registro = db.rawQuery(query, null); //objeto do sqllite que representa um objeto de qualquer tabela
            return registro;
       }

Code in class

@Override
public void onLocationChanged(Location location) {
    double lat = location.getLatitude();
    double lon = location.getLongitude();
    campoLat = (int)lat;
    campoLong = (int)lon;

    BD dbm = new BD(getApplicationContext());
    Cursor seleciona = dbm.consult(campoLat,campoLong);
    seleciona.moveToFirst();
    String aux = seleciona.getString(seleciona.getColumnIndex(BD.CADASTRO_COLUMN_NOMELOCAL));
    /*
    if(aux != "" || aux != null)
    {
        local.setText(aux);
    }
    else
    {
        local.setText("Local desconhecido");
    }*/

Bank Code

 public class BD extends SQLiteOpenHelper {
   public static final String DATABASE_NAME = "location.db";
   public static final String LOCATION_TABLE_NAME = "cadastro";
   public static final String CADASTRO_COLUMN_NOMELOCAL = "nomeLocal";
   public static final String CADASTRO_COLUMN_LATITUDE = "latitude";
   public static final String CADASTRO_COLUMN_LONGITUDE = "longitude";

public BD(Context context) {

      super(context, DATABASE_NAME , null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {

     db.execSQL(
              "create table " + LOCATION_TABLE_NAME + 
              "(" +
                CADASTRO_COLUMN_NOMELOCAL + " text primary key, " +
                CADASTRO_COLUMN_LATITUDE + " text,"+
                CADASTRO_COLUMN_LONGITUDE + " text" +
              ")"
              );

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "drop table if exists "+ LOCATION_TABLE_NAME;
    db.execSQL(query);
    onCreate(db);
}


   @Override
public void onOpen(SQLiteDatabase db) {
        String query = "drop table if exists "+ LOCATION_TABLE_NAME;
        db.execSQL(query);
        onCreate(db);
}

public boolean insert (String nome, String latitude, String longitude)
   {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();

      contentValues.put(CADASTRO_COLUMN_NOMELOCAL, nome);
      contentValues.put(CADASTRO_COLUMN_LATITUDE, latitude);
      contentValues.put(CADASTRO_COLUMN_LONGITUDE, longitude);  


      db.insert(LOCATION_TABLE_NAME, null, contentValues);
      return true;
   }

   public Cursor consult(int latiude, int longitude){
          SQLiteDatabase db = this.getReadableDatabase();
            String query = "select * from " + LOCATION_TABLE_NAME + " where " + CADASTRO_COLUMN_LATITUDE + " = " + latiude + " AND "+ CADASTRO_COLUMN_LONGITUDE + " = " +longitude;
            Cursor registro = db.rawQuery(query, null); //objeto do sqllite que representa um objeto de qualquer tabela
            return registro;
       }

}

The error occurs when I run this line

 String aux = seleciona.getString(seleciona.getColumnIndex(BD.CADASTRO_COLUMN_NOMELOCAL));

To delete the table

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "delete table if exists "+ LOCATION_TABLE_NAME;
    db.execSQL(query);
    String queryy = "drop table if exists "+ LOCATION_TABLE_NAME;
    db.execSQL(queryy);
    onCreate(db);
}
    
asked by anonymous 16.11.2014 / 21:54

1 answer

1

When using Cursor , you must first use moveToFirst . With this the cursor will point to before the first line.

To access the data, use moveToNext to check if the return was true . For example:

BD dbm = new BD(getApplicationContext());
Cursor seleciona = dbm.consult(campoLat,campoLong);

seleciona.moveToFirst();

if(seleciona.moveToNext()) {
    String aux = seleciona.getString(seleciona.getColumnIndex(BD.CADASTRO_COLUMN_NOMELOCAL));

    // Restante do código que dependa de aux
}

If you want to iterate over the results:

BD dbm = new BD(getApplicationContext());
Cursor seleciona = dbm.consult(campoLat,campoLong);

seleciona.moveToFirst();

// Eh bom fazer o cache do índice, já que ele nunca muda e evitamos processamento desnecessário
int index = seleciona.getColumnIndex(BD.CADASTRO_COLUMN_NOMELOCAL);

while(seleciona.moveToNext()) {
    String aux = seleciona.getString(index);

    // Restante do codigo que dependa de aux
}

In the case of onUpgrade , the syntax of delete is incorrect. The correct would be:

delete from nome_tabela

Remembering that the delete command does not support if exists .

In addition, since you are dropping the table, you do not have to delete the records before.

Edit:

To clear the bank at the opening of the application, simply use the class Application .

By registering a Application class you can be notified of the start and end of your application. With this you can delete the records from the table.

Just create a subclass of android.app.Application and register it in AndroidManifest with the name attribute:

<application
    android:name="nome.do.seu.pacote.NomeDaApplication">

In your% of_count% you just need to overwrite the NomeDaApplication method and clear the table:

public NomeDaApplication extends android.app.Application {

    @Override
    public void onCreate() {
        // Recupera seu banco e apaga os dados
        SQLiteDataBase bd = openOrCreateDatabase("NOME_DO_BANCO", MODE_PRIVATE, null);

        // Usar o bd para apagar as linhas
    }
}

Just be careful because I'm not sure of the execution order of onCreate and execution of SQLiteOpenHelper . I imagine it might give you an error when the bank is first created.

    
16.11.2014 / 23:31