Search code examples
androidsqliteandroid-sqlitesqliteopenhelper

Getting all database entries from android database


I have an android project, with a database where entries are logged as pairs with the same ID.

I have two for loops, one to get all entries of a certain ID, and one to get all entries in the table. These are then automatically populated into a listview.

The problem I have is that the program crashes when I attempt to use the forloop that gets all entries from the database, but it works perfectly fine when I use the for loop that wants all entries with the same ID. I have been commenting out the for loop im not using.

The for loops:

        List<Assignment> list = db.getPickupDelivery(1);
        for (int i = 0; i < list.size();i++)
            {
                allDeliveries.add(list.get(i));
            }

        List<Assignment> list = db.getAllAssignments();
        for (int i = 0; i < list.size();i++)
        {
            allDeliveries.add(list.get(i));
        }

The methods they call:

public List<Assignment> getPickupDelivery(int i) {
        List<Assignment> assignments = new LinkedList<Assignment>();

        //bygg query
        String query = "SELECT * FROM " + TABLE_ASSIGNMENTS + " WHERE id = " + i;

        //fa referens
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        //iterera och bygg och lagg till
        Assignment assignment = null;
        if (cursor.moveToFirst()) {
            do {
                assignment = new Assignment();
                assignment.setID(Integer.parseInt(cursor.getString(0)));
                assignment.setType(cursor.getString(1));
                assignment.setSenderreceiver(cursor.getString(2));
                assignment.setAdress(cursor.getString(3));
                assignment.setTime(cursor.getString(4));
                assignment.setZipcode(cursor.getString(5));
                assignment.setContact(cursor.getString(6));
                assignment.setPhone(cursor.getString(7));
                assignment.setInstructions(cursor.getString(8));

                //lagg till
                assignments.add(assignment);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("getPickupDelivery()", assignments.toString());
        return assignments;
    }

public List<Assignment> getAllAssignments() {
        List<Assignment> assignments = new LinkedList<Assignment>();

        //bygg query
        String query = "SELECT * FROM " + TABLE_ASSIGNMENTS;

        //fa referens
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        //iterera och bygg och lagg till
        Assignment assignment = null;
        if (cursor.moveToFirst()) {
            do {
                assignment = new Assignment();
                assignment.setID(Integer.parseInt(cursor.getString(0)));
                assignment.setType(cursor.getString(1));
                assignment.setSenderreceiver(cursor.getString(2));
                assignment.setAdress(cursor.getString(3));
                assignment.setTime(cursor.getString(4));
                assignment.setZipcode(cursor.getString(5));
                assignment.setContact(cursor.getString(6));
                assignment.setPhone(cursor.getString(7));
                assignment.setInstructions(cursor.getString(8));

                //lagg till
                assignments.add(assignment);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("getAllAssignments()", assignments.toString());
        return assignments;
    }

The error I get at the time of the crash is a an error that says "...MainActivity}: java.lang.NumberFormatException: Invalid int: "null""


Solution

  • My guess is that in your database definition, you didn't specify the ID column as INTEGER NOT NULL. So when you select every row, one or more of those rows has a null ID, which you're trying to parse as an int here:

    assignment.setID(Integer.parseInt(cursor.getString(0)));
    

    Change your database definition to require that the ID column is not null. You'll need to uninstall or clear data on your app so the database is recreated.

    Also another tip - Android's SQLITE implementation will enforce that you have a column called "_id", so I'd suggest using that, not "id". Otherwise you'll end up with an extra column you don't need.