how to get the database value to a String array in android(sqlite Database)

50,911

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
Share:
50,911
Ramz
Author by

Ramz

Android Application Developer Technopark Trivandrum(2011-2013) Now Working for Voltella Co-Founder(2013 to present) And l love my Nephew Munnachiii and Niece Meenuttiii *Won the Google Biz Droid competition 2014 banglore(Google) *2nd place for Angelhack 2013 banglore (Microsoft) Github Profile gist profile LinkedIn My Posts

Updated on December 30, 2021

Comments

  • Ramz
    Ramz over 2 years

    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.