mardi 5 avril 2016

Finalising earlier cursors from sqlite database

I am getting a finalise Cursor/Database error from the following code. The app is running a SQLite FTS4 database with sqlcipher encryption. I have read ALL the StackOverflow questions / answers i can find including one that is very simular to this but does not resolve my problem. Help please, spent three days trying to resolve this now. I have tried many combinations of closing previous instances of the Cursor/database and i thought changeCursor closed previous cursors?. I do need to keep the current cursor open and the warnings are for all previous searchview selections that call a query. I know there was an early problem with sqlcipher not closing the database even when Close() was called but i believe this was resolved years ago. The code; Apart from this everything works as I want!

DBHandler class querying the database .......

public static SQLiteDatabase ourdb; 
    private final Context cont;
    public Cursor Cursor;

        public DBHelper(Context context) {
            super(context, DB_NAME, null, version);
            this.cont = context;
            SQLiteDatabase.loadLibs(context);
        }

    public void onCreate(SQLiteDatabase ourdb) {

 .....   Code to load database
}

 @Override
   public void onUpgrade(SQLiteDatabase ourdb, int oldVersion, int newVersion) {
 }


   public void closeCursor() {
                if (Cursor != null) {
                        Cursor.close();
        }
        }


    public void open(){
        if (ourdb == null) {
            ourdb = this.getReadableDatabase(pswrd);
        }
    }

    public void close() {
        super.close();
        if (ourdb != null) {
            if (ourdb.isOpen()) {
                ourdb.close();
            }
        }
    }

      public Cursor searchByArtistText(String inputText) throws SQLException {
        open();
        closeCursor();
                String query = "Select _id, recno, title, publisher from " + DB_TABLE + " where title like " + inputText + ";" + "order by title, publisher";
    Cursor Cursor = ourdb.rawQuery(query, null);
    if (Cursor != null) {
        Cursor.moveToFirst();
        close();
            }
    return Cursor; 
}

This is the query calling method from a separate class;

      private void displayResults(String query) {
         requery = query;
        final Cursor cursor = DbHelper.searchByTitle((query != null ? query : "AAAAA"));
        if (cursor != null) {
                    String[] from = new String[]{DBHelper.Recno,
                    DBHelper.Title,
                    DBHelper.Publisher,
                      };

            // Specify the view where we want the results to go
            int[] to = new int[]{R.id.srecno,
                    R.id.stitle,
                    R.id.spublisher,
                    };

            // Create a simple cursor adapter and return the database query into it
            SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.result_search_item, cursor, from, to, 0);
            myList.setAdapter(cursorAdapter); 
            cursorAdapter.changeCursor(cursor);

            // Wait for the user to select a record
            myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // Get the cursor, positioned to the corresponding row in the result set
                    Cursor cursor = (Cursor) myList.getItemAtPosition(position);
                    recno = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                    DbHelper.closeCursor();
                 //   cursor.close();
                    displayDetail(recno);
                }
            });
        }
     }

Aucun commentaire:

Enregistrer un commentaire