0

I want to store the values of a particular column in the array, As I am a fresher I don't know how to do this. I am getting values from sqlite as

1
2
123
432
3
5

I want to store these values in string array. Please tell me I am not finding any appropriate example by googling about this.. thanx in advance.

public void fun(String query){
    Cursor cursor = db.rawQuery(query, null);

    try{
        String[] arr = new String[cursor.getCount()];
        if(cursor != null){
            while(cursor.moveToNext()){
                for(int i = 0; i < cursor.getCount(); i++){
                    arr[i] = cursor.getString(0).trim();

                }
                cursor.moveToNext();
            }
            cursor.close();
        }

    }finally{
        cursor.close();
    }

}

Here query is

SELECT <COLUMN_NAME> FROM <TABLE_NAME> WHERE <CONDITION>;

I think I am doing it wrong please correct my errors...

1
  • 1
    Put your code how you getting these values from array, so we can fill these in array for you. Commented Jul 10, 2012 at 11:34

3 Answers 3

2

I consider using rawQuery is a bad habit, try to avoid this(except in extreme cases)

Try as follows to solve your problem, hope this will help you:

public ArrayList<String> getAllStringValues() {
    ArrayList<String> yourStringValues = new ArrayList<String>();
    Cursor result = db.query(true, YOUR_TABLE,
            new String[] { YOUR_COLUMN_NAME }, null, null, null, null,
            null, null);

    if (result.moveToFirst()) {
        do {
            yourStringValues.add(result.getString(result
                    .getColumnIndex(YOUR_COLUMN_NAME)));
        } while (result.moveToNext());
    } else {
        return null;
    }
    return yourStringValues;
}

Use this method in YourCustomDBManager class. consider NotePad example of android developers sites example programmers guide for getting better concept. It will help you to learn how to deal with SQLite. I am also new in Android, but I learned everything about SQLite from NotePad example.

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

Comments

1
Vector<String> vecInt = new Vector<String>; // you can use any datatype <int><float>

cursor.moveToFirst();
for(i=0;i<cursor.getCount();i++)
{
      vecInt.add(cursor.getString(COLUMN_NUM));// if you are using datatype other then string then need to convert here
}

Comments

1
int [] val = new int[cursor.getCount()]; // integer array
for(int i= 0; i<cursor.getCount(); i++)
     val[i] = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME));

1 Comment

I have put my code doing somehow like this but not getting exact answer.. please check

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.