Convert CrossProcessCursorWrapper to SQLiteCursor

I have code

SQLiteCursor cur = (SQLiteCursor)CApp.Db.rawQuery(myQuery, null);

and it works for non-Cipher version. When I use SQLCipher classes it throws ClassCastException.
Can you tell me how to change code? How to create SQLiteCursor using Database.rawQuery()?

SQLCipher for Android has its own SQLiteCursor class. You cannot create a framework SQLiteCursor class from a SQLCipher for Android query result. You will need to change your import statements to use net.sqlcipher.database.SQLiteCursor.

I use SQLiteCursor from net.sqlcipher.databaseAQLiteCursor;
Code compiles but throws ClassCastException when executing.
Error message is “java.lang.ClassCastException net.sqlcipher.CrossProcessCursorWrapper cannot be cast to android.database.CrossProcessCursorWrapper”

The code in your first post does not do a cast to CrossProcessCursorWrapper. It does a cast to SQLiteCursor.

I suggest that you post the complete Java stack trace for your crash, along with all of your code that is referenced in that stack trace.

A code sufficient to reproduce the error

import net.sqlcipher.database.SQLiteCursor;

SQLiteCursor cur = null;
try
{
cur = (SQLiteCursor)CApp.Db.rawQuery(myQuery, null);
while(cur.moveToNext())
{
}
}
catch(Exception ex)
{
}

I found solution using that code:
cur = (SQLiteCursor)((CrossProcessCursorWrapper)CApp.Db.rawQuery(query, args)).getWrappedCursor();
All classes are from SQLiteCipher.

Hi @korwinek

In this case, you should just be able to reference the net.zetetic.Cursor interface instead which would remove your need of get the wrapped cursor and performing a cast. An example can be found in our test suite here.

Example shows how to create net.zetetic.Cursor, but I need to create net.zetetic.SQLiteCursor object which allows me to call method setSelectionArguments() and then method requery().

Hello @korwinek

Your example above only showed the usage of moveToNext which would be satisfied by the interface, however if you do need setSelectionArguments you would need to cast to the concrete instance.

@developernotes
Thank you for help and quick response.

I converted all hundreds of thousands of code lines to use SQLCipher. It compiles :grinning: and now it’s time for testing.

Hello @korwinek

We are happy to help, take care!