7

I have a database name "CUED" (sqlite Android)it have a table HELLO which contain a column NAME I can get the value to String from that column. Let me show you my code section

myDB =hello.this.openOrCreateDatabase("CUED", MODE_PRIVATE, null); 
Cursor crs = myDB.rawQuery("SELECT * FROM HELLO", null);
                
while(crs.moveToNext())
{
    String uname = crs.getString(crs.getColumnIndex("NAME"));
    System.out.println(uname);
}

It will print the value one by one. Now what I need is that I want to get the column values from database and so that I can store it in a String Array.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ramz
  • 7,116
  • 6
  • 63
  • 88

3 Answers3

25

You already did the hard part... the array stuff is pretty simple:

String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
    String uname = crs.getString(crs.getColumnIndex("NAME"));
    array[i] = uname;
    i++;
}

Whatever, I always recommend to use collections in cases like this:

List<String> array = new ArrayList<String>();
while(crs.moveToNext()){
    String uname = crs.getString(crs.getColumnIndex("NAME"));
    array.add(uname);
}

In order to compare the arrays, you can do things like this:

boolean same = true;
for(int i = 0; i < array.length; i++){
    if(!array[i].equals(ha[i])){
        same = false;
        break;
    }
}
// same will be false if the arrays do not have the same elements
Alois Heimer
  • 1,772
  • 1
  • 18
  • 40
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • thank you bro now can help me one more time i have another string array i want to compare both value in the array let me explain my first array--- String[] ha={"hello","hai"}; and String array from database----String[] array={"hello","hai"}; i want to check the each position of both array if both are same let me enter next page – Ramz Jan 25 '12 at 14:42
  • Good , but if need to get all the column values from only one row to form string array means how to do ? – Subha Apr 16 '13 at 10:05
  • Hey, Great answer... What to do in case multiple rows are returned. ?? String uname = crs.getString(crs.getColumnIndex("NAME")); String lname = crs.getString(crs.getColumnIndex("LASTNAME")); – Tushar Narang Jul 16 '15 at 08:54
  • 1
    Don't forget to `crs.close();` – PhilipB Nov 06 '16 at 14:48
0

This is my code that returns arraylist contains afield value:

public ArrayList<String> getAllPlayers() {

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT " + serailnumber + " as _id, " + title
            + " from " + table, new String[] {});
    ArrayList<String> array = new ArrayList<String>();
    while (cur.moveToNext()) {
        String uname = cur.getString(cur.getColumnIndex(title));
        array.add(uname);

    }
    return array;
}
Luke Beveridge
  • 505
  • 1
  • 6
  • 19
Ribin Haridas
  • 1,630
  • 11
  • 17
0
       String[] str= new String[crs.getCount()];
       crs.movetoFirst();           
     for(int i=0;i<str.length();i++)
        { 
           str[i] = crs.getString(crs.getColumnIndex("NAME"));
            System.out.println(uname);
      crs.movetoNext();
        }

Enjoy it

Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44