-1

I want to search my database for a row where KEY_TOR == place.getTor()

Here I call the method:

DB_Place tor = db_tore.getDBPlace(place.getTor());

This is the method:

public DB_Place getDBPlace(String name) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_DB_TORE_Eintrag, new String[] { KEY_ID,
                    KEY_PLACE_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE, KEY_TOR }, KEY_TOR + "=?",
            new String[]{name}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    DB_Place place = new DB_Place(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));

    return place;
}

Looks good to me, except that I am getting this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.x.x/com.example.ahok.x.UI_MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

What am I missing here? Is it possible that it has something to do with a few of the columns being null?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Glave
  • 137
  • 1
  • 1
  • 10

1 Answers1

1

Looks like you don't have any data inserted in your database. Moreover, you've some logical error in your code.

I would like to edit your function like this.

public DB_Place getDBPlace(String name) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_DB_TORE_Eintrag, new String[] { KEY_ID,
                    KEY_PLACE_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE, KEY_TOR }, KEY_TOR + "=?",
                    new String[]{name}, null, null, null, null);

    DB_Place place = null;

    if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {

        place = new DB_Place(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
    }

    return place;
}

Update

So if you think its an error of your query, then run simple query like this.

Cursor cursor = db.rawQuery("Select * from " + TABLE_DB_TORE_Eintrag + " where " + KEY_TOR + " = '" + name + "'", null);
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • I took your code and at least the app does not crash anymore. The method does indeed return that `place` is null, even though the database is not empty. I checked it with a method the returns the whole database. – Glave Nov 12 '16 at 18:12
  • Print all the rows you have in your `TABLE_DB_TORE_Eintrag` and post it here. – Reaz Murshed Nov 12 '16 at 18:17
  • I think that maybe the where clause doesnt work, but I dont get why. `name` is "Tor 1" and as I checked in the database exists an entry where `KEY_TOR` is "Tor 1" – Glave Nov 12 '16 at 18:18
  • Please see the update and let me know if that helps. – Reaz Murshed Nov 12 '16 at 18:22
  • I am not sure how to print them, but this is the return array of a method that gets all the data in that database: [Image of the Debugger](http://www.pic-upload.de/view-32105081/debugger.png.html) – Glave Nov 12 '16 at 18:25
  • Please try the updated query. I'm taking a look at the debugger. – Reaz Murshed Nov 12 '16 at 18:26
  • This exceptions happens with the new query: "Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: Select * from orte where tor = Tor 1" – Glave Nov 12 '16 at 18:29
  • The image of the debugger does not load for me. [Reupload](https://i.imgsafe.org/75fc8ccb27.png) – Glave Nov 12 '16 at 18:32
  • @Glave You need to quote the name: `select * from orte where tor = 'Tor 1'` – Karakuri Nov 12 '16 at 18:33
  • Yes, i realized that a minute ago, too. I am such an idiot. This happens when you are coding for too long in one sitting. Thank you for getting me there! – Glave Nov 12 '16 at 18:36
  • That's why we prefer using the parametrized queries. The string quoting is automatic. And you're less prone to SQL injection. – Phantômaxx Nov 12 '16 at 18:37