mercredi 15 avril 2015

Cursor in Android having issues with SqliteDatabase

This is the Adapter Class I am using to Insert or Get Data from the SQLite Database. There is a method for inserting the data and a method for getting the data.


In the getData method, I return a cursor and handle it in some other class but the problem I face is difficult to understand from my perspective.


When I get the data using the query function of SqliteDatabase it returns me a Cursor so, in other words, the cursor has a subset of the some table inside the database. The work of Database Object is over so I close it and then return the Cursor. The function works without any problem but the cursor returned on the other side is empty.


Log displayed:



java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.



If I remove the line:



db.close();



then everything works fine, but some point in time it says leak in the database if I call getData() function more number of times.



public class DatabaseAdapter {


DatabaseHelper helper;

public DatabaseAdapter (Context context) {

helper = new DatabaseHelper(context);



}

public long insertData(String username, String domain, String crypt) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.USERNAME, username);
values.put(DatabaseHelper.DOMAIN, domain);
values.put(DatabaseHelper.CRYPT, crypt);
long id = db.insert(DatabaseHelper.TABLE_NAME, null, values);
db.close();
return id;
}

public Cursor getData() {
String[] columns = {DatabaseHelper.USERNAME, DatabaseHelper.DOMAIN, DatabaseHelper.CRYPT};
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
db.close(); // This line is the problem.

return c;

}

public boolean deleteRow(SavedInformation information) {
String[] array = {information.getUsername(), information.getDomainName(), information.getCryptPassword()};
SQLiteDatabase db = helper.getWritableDatabase();
db.delete(DatabaseHelper.TABLE_NAME, "" + DatabaseHelper.USERNAME + " = ? AND " + DatabaseHelper.DOMAIN + " = ? AND " + DatabaseHelper.CRYPT + " = ?", array);

return true;
}


static class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "Data_HASH";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "HASH";

/*DATABASE COLUMNS */
private static final String UID = "_id";
private static final String USERNAME = "username";
private static final String DOMAIN = "domain";
private static final String CRYPT = "crypt";


/*DATABASE CREATE QUERY */
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + USERNAME + " VARCHAR," + DOMAIN + " VARCHAR," + CRYPT + " VARCHAR);";

public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
} catch (SQLException e) {
e.printStackTrace();
}
}
}}

Aucun commentaire:

Enregistrer un commentaire