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);
}