0

I have certain data in sqlite and want to fetch the data into an array of string, I am getting the first column successfully but cant able to fetch the next column data. Here is my code.

SQLiteDatabase db =  contactsdbHelper.getWritableDatabase();
    //db.openDatabase(path, factory, flags)
     int columnIndex = 1; // Whichever column your float is in
   Cursor cursor1= getData();
   String[] values=new String[cursor.getCount()+1];
   try{
   if(cursor1.moveToFirst())
   {
       for(int i=0;i<cursor1.getCount();i++ )
       {
           values[i]=cursor1.getString(columnIndex);
           //Log.i("HI", "piyush");
           Log.i("StartTime"+i, values[i]);

           cursor1.moveToNext();
       }
   }
   }
   catch(ArrayIndexOutOfBoundsException e){
       Log.i("Exception Generated", e.toString());

   }
   String[] values1=new String[cursor.getCount()+1];
   try{
   if(cursor1.moveToNext())
   {
       for(int i=0;i<cursor1.getCount();i++ )
       {
           values1[i]=cursor1.getString(columnIndex+1);

           Log.i("EndTime"+i, values[i]);

           cursor1.moveToNext();
       }
   }
   }
   catch(ArrayIndexOutOfBoundsException e){
       Log.i("Exception Generated", e.toString());

   }
1
  • Please mark correct answers; it encourages people to respond. Commented Mar 3, 2011 at 23:19

5 Answers 5

1

Why not fill both arrays at once?

SQLiteDatabase db =  contactsdbHelper.getWritableDatabase();
//db.openDatabase(path, factory, flags)
 int columnIndex = 1; // Whichever column your float is in
Cursor cursor1= getData();
String[] values=new String[cursor.getCount()+1];
String[] values1=new String[cursor.getCount()+1];
try{
if(cursor1.moveToFirst())
{
   for(int i=0;i<cursor1.getCount();i++ )
   {
       values[i]=cursor1.getString(columnIndex);
       values1[i]=cursor1.getString(columnIndex+1);
       Log.i("StartTime"+i, values[i]);

       cursor1.moveToNext();
   }
}
}
catch(ArrayIndexOutOfBoundsException e){
   Log.i("Exception Generated", e.toString());

}

But that whole thing looks kinda rubbish ... what are you trying to achieve?

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

2 Comments

want to fetch the sqldata into an array, i tried this code also but again i am able to fetch the column 1 data in both the arrays.want to fetch the next column data into value1 array.
I think the value of columnIndex is not incremented in second array values1[] i tried a lot but even give static value 2 in values1[i]=cursor1.getString(2); still not able to fetch the second column data into values1 again it showing the column1 data.
1

You will need to call moveToFirst() on the Cursor again before you can start reading the second column.

Specifically the second if should be:

if (cursor1.moveToFirst())

Edit - usually data is in a database for a reason: you don't want to fetch it all into memory in one big array most of the time.

Could you be looking for a CursorAdapter and ListView combination to display the data in a list?

3 Comments

yah actually but my boss want to show the data into table layout manner and for these i have to first fetch the data into an array to show it into table layout.
i tried the below code also but again the second array is fetching the first column data.
your query might be wrong then. Are you sure you are actually getting columns with different data?
0

Try reading the column values by their column name.

Cursor cursor = ...
if(cursor.moveToFirst()) {
    do {
         values[i] = cursor.getString(cursor.getColumnIndex("columnName"));
         values1[i] = cursor.getString(cursor.getColumnIndex("otherColumnName"));
    } while(cursor.moveToNext());
}

where "columnName" and "otherColumnName" are the names of the columns of your table which you want to fill into the arrays.

1 Comment

Thanks for your reply got the solution.Above code is correct i was doing some foolish things with the data part.
0

Cursor cur = db.query(TABLENAME, null, null, null, null, null, null);
        cur.moveToFirst();
        int a[] = new int[100];

for (int i = 0; i < cur.getCount(); i++) { a[i] = cur.getInt(0); cur.moveToNext(); }

Comments

0

Here is my code to return all the data in the table in list. But, it is not working. I have called this method from CheckData class which in turn is called by main class via intent.

public List<String[]> selectAll()
{
    List<String[]> list = new ArrayList<String[]>();
    Cursor cursor = db
      .query(TABLE_NAME, null, null, null, null, null, null);
    int x = 0;
    if (cursor.moveToFirst())
    {
        do
        {
            String[] b1 = new String[]
            {
                cursor.getString(1),
                cursor.getString(2)
            };
            list.add(b1);
            x = x + 1;
        }
        while (cursor.moveToNext());
    }

    if (cursor != null && !cursor.isClosed())
    {
        cursor.close();
    }
    cursor.close();
    return list;
}

My database contains 3 columns - id(integer primary key), symbol(text) and company_name(text). My database name is AppDB and table name is scrip.

Comments

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.