質問

In Android I query some data from the database into a cursor,after it I close the database. After it when use cursor method it gives error. Anyone can explain why?

役に立ちましたか?

解決

This is not really a direct answer, but it is an example of how to do what you are talking about. I have a class called ContactDataSource that allows access and manipulation of my database via a Cursor. To do this, the database needs to be able to be opened and closed. This is handled in my class via the following methods

public void open() throws SQLException{
    db = helper.getWritableDatabase();
}

public void close(){
    helper.close();
}

I need to be able to get all of my contacts, so that I can utilize their variables, modify them, etc. It would be inefficient, not to mention not very secure, to keep my database connection open the whole time I need the variables in question. I need to store the information from my database somewhere, somehow, on my device. In order to do that I need to call the following method

private String[] allColumns = {ContactDataSQLHelper.COLUMN_ID, ContactDataSQLHelper.COLUMN_CONTACT_NAME,
        ContactDataSQLHelper.COLUMN_CONTACT_ADDRESS, ContactDataSQLHelper.COLUMN_CONTACT_NUMBER};
...

public ArrayList<ContactObject> getAllContacts(){
    ArrayList<ContactObject> contacts = new ArrayList<ContactObject>();

    // Again, without our Cursor, we can't actually point at any of the data we want to
    // work with/manipulate
    Cursor cursor = db.query(ContactDataSQLHelper.TABLE_CONTACTS, allColumns, 
            null, null, null, null, null);
    cursor.moveToFirst();

    while(!cursor.isAfterLast()){
        ContactObject contact = cursorToContact(cursor); 
        contacts.add(contact);
        cursor.moveToNext();
    }

    cursor.close();
    return contacts;
}

which will return my list of contacts in the form of ContactObjects. You can see that this method will close our cursor's database connection once it is done. However, before it is closed, it calls cursorToContact(cursor) which will allow us to use our cursor to create a readable ContactObject

private ContactObject cursorToContact(Cursor cursor){
    int id = cursor.getInt(0);

    String name = cursor.getString(1);
    String address = cursor.getString(2);
    String number = cursor.getString(3);

    return new ContactObject(name, address, number, id);
}

So in short: Open Connection -> Get what you need from your database -> Store it in an object/variable -> Close connection is the process that needs to be taken.

This can be achieved by call open method -> call getter method -> (if needed) call helper method -> call close method

It is important to note that if you want to reverse the process (save the modified database information) a similar process is followed, just using database setter methods instead of getter methods.

他のヒント

This is expected behavior. Cursors require the database to be open in order to be able to read their data.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top