Ramz Ramz - 1 year ago 92
Android Question

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

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);

String uname = crs.getString(crs.getColumnIndex("NAME"));

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.

Answer Source

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

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

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

List<String> array = new ArrayList<String>();
    String uname = crs.getString(crs.getColumnIndex("NAME"));

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

boolean same = true;
for(int i = 0; i < array.length; i++){
        same = false;
// same will be false if the arrays do not have the same elements
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download