Cognibuild
Cognibuild

Reputation: 479

Closing DataSource and Using Cursor

I have a method which essentially does the following:

Cursor cursor;                                  
cursor = myCard.queryRandomCard(this);              
cursor.moveToFirst();

Within the queryRandomCard method:

DataSource datasource = new DataSource(context);
datasource.open();
Cursor cursor  = (SQL STATEMENT IS HERE AND WORKS); 
datasource.close();
return cursor;

The problem lay in that, upon retutn I get an error while executing "cursor.moveToFirst()" which says "the connection pool has been closed". HOWEVER, if I remove the "datasource.close()" call, then it works fine.

My question is, why does closing the database here affect the cursor that is returned? Are these two tied together closer than I had realized?

Thank you

Upvotes: 1

Views: 151

Answers (1)

Idipaolo
Idipaolo

Reputation: 788

A Cursor is nothing more than an iterator that operates on the tables of Database. With a Cursor you don't load the whole table at once, but you load one row each time you need it, so you need an open connection to the database in order that works.

Upvotes: 2

Related Questions