0

I'm trying to get from a SELECT, the values from 2 columns in my SQLite Database, and when I retrieve it, I want to add this values in a custom ArrayList that I created that have (String, String) parameters.

My DB Helper method is:

public ArrayList<Custom> getAllvalues(String TABLE, String COLUMN1, String COLUMN2) {

        ArrayList<Custom> list = new ArrayList<Custom>();

        // Select All Query
        String selectQuery = "SELECT "+COLUMN1+","+COLUMN2+" FROM " + TABLE;

        SQLiteDatabase db = this.getWritableDatabase();
        try {

            Cursor cursor = db.rawQuery(selectQuery, null);
            try {

                // looping through all rows and adding to list
                if (cursor.moveToFirst()) {
                    do {
                        list.add(new Custom(cursor.getString(0),cursor.getString(1)));

                    } while (cursor.moveToNext());
                }

            } finally {
                try { cursor.close(); } catch (Exception ignore) {}
            }

        } finally {
             try { db.close(); } catch (Exception ignore) {}
        }

        return list;
    }

When I get the values, in my main class.. I get like this:

VALUE1 NULL;
NULL  VALUE2;
VALUE3 NULL;
NULL VALUE4;

and I want that the values be in the list like this:

VALUE1 VALUE2;
VALUE3 VALUE4;

I tried to acces to the cursor like this:

cursor.getColumnIndex("My_column_name");

And I retrieved the values, but always in the same order.. value,null,null,value,value,null,etc...

Thank you!

2
  • 1
    Which is the content from your table? Because from your code, what you get is exactly what you are asking the DB for. How can you be sure that each row has one null value and one non-null value? Or that the rows will be returned in exactly the order you want? It seems your relational model is pretty much broken. Commented Jul 28, 2014 at 21:42
  • Do you have either the raw database dump or the code that has entered the rows into the db? Commented Jul 28, 2014 at 21:42

1 Answer 1

1

Try to check your database to see if the rows you are getting are populated with the data you are trying to retrieve. Look this answer to export your .db file. And download SQLite Debugger to open it to find out the information inside the database.

If your information is correctly persisted try to get the information by calling the query method of SQLiteDatabase:

Cursor cursor = db.query(TABLE, new String[]{COLUMN1, COLUMN2}, null, null, null, null, null}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.