0

Im trying to get the data of an entire column into a string array. My database contains two columns Id and Names. I want to read all the entries of the names column and put it into a array. Please help.

EDIT #1: Im using the following code but i can get only one name with this code.

String query = "Select * FROM " + TABLE_APPS + " WHERE " + COLUMN_NAME + " = \"" + productname + "\""; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(query, null); if (cursor.moveToFirst()) { cursor.moveToFirst(); name = cursor.getString(1); cursor.close(); } else { name = null; } db.close(); 
3
  • 2
    First Try Something,if you could not get the answer then share your effort and ask help. We will ready to help you. Anyhow,Try This: Run the select query and get the result in cursor.Get the cursor count and initialize the array with size cursor count. Now Iterate the cursor and put into . Commented Nov 8, 2013 at 6:27
  • 1
    you are not performing any loop to get all the values from table.The if statement excutes only once,so only you are getting only one name Commented Nov 8, 2013 at 6:57
  • @krishna,stackoverflow.com/questions/26808829/… Commented Nov 7, 2014 at 22:09

1 Answer 1

1
 int total=0; Cursor csr=sdb.query("tablename", null, null,null,null,null,null); csr.moveToFirst(); while(!csr.isAfterLast()) { total++; csr.moveToNext(); } String strarray[] = new String[total]; Cursor csrs=sdb.query("tablename", null, null,null,null,null,null); csrs.moveToFirst(); int aray=0; while(!csrs.isAfterLast()) { strarray[aray]=csrs.getString(1); aray++; csrs.moveToNext(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but it doesnt work. When i run the app it says [Ljava.lang.String;@41852260
Ljava.lang.String;@ is returned where you used string arrays as strings.
Worked wrote a loop to get the data. Thanks
Can I get all values and not just last entry?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.