360Productions
360Productions

Reputation: 37

Data from Sql Database to an Array

I would like to put some data from a Sql database in an Array

Here is my code for the array:

openDB();
Cursor c = myDb.getSpalte();
while (!c.isAfterLast()) 
     {
         s1nInt[i]  = c.getInt(1);
         i++;
         c.moveToNext();
     }
c.close();
closeDB();

And here is my code for the getSpalte(); Method:

public Cursor getSpalte()
{
    String where= null;
    Cursor c =  db.query(true, DATABASE_TABLE, KEY_KALOA, 
            where, null, null, null, null, null);
    if (c != null) 
    {
      c.moveToFirst();
    }
  return c;
}

And if I run this I get these Exception:

06-27 13:34:01.021: E/CursorWindow(24334): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.

now I got for these command an exception:

  Number[] series2Numbers = {/*s1nInt[i-6],s1nInt[i-5],s1nInt[i-4],s1nInt[i-3],s1nInt[i-   2],s1nInt[i-1],*/s1nInt[i]};

This is my Exception for this:

Graph.onStart() line: 75    
Graph(Fragment).performStart() line: 1801   
FragmentManagerImpl.moveToState(Fragment, int, int, int, boolean) line: 937 
FragmentManagerImpl.moveToState(int, int, int, boolean) line: 1106  
BackStackRecord.run() line: 690 
FragmentManagerImpl.execPendingActions() line: 1571 
FragmentManagerImpl$1.run() line: 447   
Handler.handleCallback(Message) line: 733   

Upvotes: 0

Views: 67

Answers (1)

Brett Okken
Brett Okken

Reputation: 6306

The cursor starts at 0 and must be moved to the first row before accessing data:

     while(c.moveToNext())
     {
         s1nInt[i]  = c.getInt(1);
         i++;
     }

Upvotes: 1

Related Questions