Hi,
In the past few months I've been learning how to make applications for android by making small games just for the fun of it. I am stuck at the moment at the database object I created to manage the database, specifically the cursor: it won't close. A snippet of my code:
public class DatabaseManager extends Activity
{
// the Activity or Application that is creating an object from this class.
Context context;
// a reference to the database used by this application/object
private SQLiteDatabase db;
/**********************************************************************
* RETRIEVING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to retrieve a row from a database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the id of the row to retrieve
* @return an array containing the data from the row
*/
public ArrayList<Object> getRowAsArray(long rowID)
{
// create an array list to store data from the database row.
// I would recommend creating a JavaBean compliant object
// to store this data instead. That way you can ensure
// data types are correct.
ArrayList<Object> rowArray = new ArrayList<Object>();
try
{
Cursor cursor;
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
startManagingCursor(cursor);
// move the pointer to position zero in the cursor.
cursor.moveToFirst();
// if there is data available after the cursor's pointer, add
// it to the ArrayList that will be returned by the method.
if (!cursor.isAfterLast())
{
do
{
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getLong(1));
}
while (cursor.moveToNext());
}
// let java know that you are through with the cursor.
cursor.close();
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
// return the ArrayList containing the given row from the database.
return rowArray;
}
}
I keep getting the error after some time each time that function is called:
10-02 16:07:23.618: WARN/SQLiteCompiledSql(3945): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: UPDATE nw_table SET value=?, id=? WHERE id=9
10-02 16:07:23.618: WARN/SQLiteCompiledSql(3945): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
And then resulting in a database crash:
10-02 16:07:23.748: ERROR/Database(3945): close() was never explicitly called on database '/data/data/com.deltac.neonwars/databases/database_neon_wars'
10-02 16:07:23.748: ERROR/Database(3945): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
Does anyone know how I can fix this?
Thanks in advance,
~G