Regarding sqlcipher class cast exaception & missing classes in jar

196 views
Skip to first unread message

A sayyad

unread,
Jun 1, 2012, 7:47:06 AM6/1/12
to SQLCipher Users
Hi All,

I want to encrypt sqlitedatabase in android so I thought of using
SQLcipher guardian project.

I referred the following link

https://guardianproject.info/code/sqlcipher/

The link says---

Update the import path from android.database.sqlite.* to
info.guardianproject.database.sqlite.* in any source files that
reference it. The original android.database.Cursor can still be used
unchanged.
If you change import from android.database.sqlite to
info.guardianproject.database.sqlite.* means all the classes present
in the android.database.sqlite must be present in the package
info.guardianproject.database.sqlite.
But after checking the jar, I found some classes are missing in the
info.guardianproject.database.sqlite which are as follows--

SQLiteAccessPermException
SQLiteBindOrColumnIndexOutOfRangeException
SQLiteBlobTooBigException
SQLiteCantOpenDatabaseException

SQLiteDatabaseLockedException

SQLiteDatatypeMismatchExceptionSQLiteTableLockedException

Wont it give me problem if I want to use missing classes- ??????



2)Second thing is --->

SQLiteCursor cursor =(SQLiteCursor)sqldb.rawQuery((String)params[1],
strArgs);

When I use above casting,the code works fine with native android code.

But when I use info.guardianproject.database.sqlite classes, I get
"Class cast exception"

Please reply.

Thanks & Regards,

A Sayyad


Billy Gray

unread,
Jun 1, 2012, 9:49:45 AM6/1/12
to sqlc...@googlegroups.com
Hi A,

The SQLCipher project, and the Android integration, have changed a bit since that page was put together. We have an updated tutorial on integrating SQLCipher for Android into your app over here:


One of the biggest differences is that the namespace we use is net.sqlcipher.*.

Cheers,
Billy Gray
--
Team Zetetic
http://zetetic.net

Nathan of Guardian

unread,
Jun 1, 2012, 9:54:27 AM6/1/12
to sqlc...@googlegroups.com
On 06/01/2012 09:49 AM, Billy Gray wrote:
>
> The SQLCipher project, and the Android integration, have changed a bit
> since that page was put together. We have an updated tutorial on
> integrating SQLCipher for Android into your app over here:
>
> http://sqlcipher.net/sqlcipher-for-android/

I have updated the page on the Guardian site to point to the proper
place now and provide the correct details.

A sayyad

unread,
Jun 1, 2012, 9:56:05 AM6/1/12
to SQLCipher Users
Thanks..
But I imported that jar also.But I am getting same classcast exception
when I try to use following api

SQLiteCursor cursor =(SQLiteCursor)sqldb.rawQuery((String)params[1],
strArgs);

& same below classes are missing in that package...

Billy Gray

unread,
Jun 1, 2012, 10:03:56 AM6/1/12
to sqlc...@googlegroups.com
Hi A,

That's not a short tutorial, might I suggest you start a new test project from scratch, following the steps outlined there? Or have you done that already?

B

Mark Murphy

unread,
Jun 1, 2012, 10:05:42 AM6/1/12
to sqlc...@googlegroups.com
On Fri, Jun 1, 2012 at 9:56 AM, A sayyad <ashiq...@gmail.com> wrote:
> Thanks..
> But I imported that jar also.But I am getting same classcast exception
> when I try to use following api
>
> SQLiteCursor cursor =(SQLiteCursor)sqldb.rawQuery((String)params[1],
> strArgs);

Replace:

SQLiteCursor

with:

Cursor

Your code should neither know nor care what implementation of the
Cursor interface is handed back to you.

That being said, either:

- You are not importing SQLiteCursor from net.sqlcipher, or
- sqldb is not a net.sqlcipher.SQLiteDatabase

> & same below classes are missing in that package...

Those are all new to API Level 11. Presumably, SQLCipher does not
throw them. I do not even see any evidence that Android throws them.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 3.7 Available!

A sayyad

unread,
Jun 1, 2012, 10:25:56 AM6/1/12
to SQLCipher Users
Thanks.

But when I am using android sdk rawQuery api and casting cursor to
SQLiteCursor ,its working fine without any problem.
But when I use sqlcipher provided rawQuery api and casting cursor to
SQLiteCursor ,I am getting class cast exceptin.
I took the latest zip file from sqlcipher site

On Jun 1, 7:05 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> On Fri, Jun 1, 2012 at 9:56 AM, A sayyad <ashiqsay...@gmail.com> wrote:
> > Thanks..
> > But I imported that jar also.But I am getting same classcast exception
> > when I try to use following api
>
> > SQLiteCursor cursor =(SQLiteCursor)sqldb.rawQuery((String)params[1],
> > strArgs);
>
> Replace:
>
> SQLiteCursor
>
> with:
>
> Cursor
>
> Your code should neither know nor care what implementation of the
> Cursor interface is handed back to you.
>
> That being said, either:
>
> - You are not importing SQLiteCursor from net.sqlcipher, or
> - sqldb is not a net.sqlcipher.SQLiteDatabase
>
> > & same below classes are missing in that package...
>
> Those are all new to API Level 11. Presumably, SQLCipher does not
> throw them. I do not even see any evidence that Android throws them.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy

Mark Murphy

unread,
Jun 1, 2012, 10:37:02 AM6/1/12
to sqlc...@googlegroups.com
On Fri, Jun 1, 2012 at 10:25 AM, A sayyad <ashiq...@gmail.com> wrote:
> But when I am using android sdk rawQuery api and casting cursor to
> SQLiteCursor ,its working fine without any problem.
> But when I use  sqlcipher provided  rawQuery api and casting cursor to
> SQLiteCursor ,I am getting class cast exceptin.
> I took the latest zip file from sqlcipher site

As I wrote (now with a typo fix), either:

- you are not importing SQLiteCursor from net.sqlcipher.database, or

- sqldb is not a net.sqlcipher.database.SQLiteDatabase

--

ashiq sayyad

unread,
Jul 25, 2012, 4:50:04 AM7/25/12
to sqlc...@googlegroups.com
I have attached the sample project.

On clicking button,Read From DB,
I am  displaying a toast with database data & database version using following code snippet..

public void onClick(View v) {
// TODO Auto-generated method stub
try{ 
SQLiteCursor c =(SQLiteCursor) db.getDBData();
//Cursor c = db.getDBData();
if(c !=  null && c.getCount() > 0){
Log.i("SQLCipher", "Cipher cursor count"+c.getCount() );
//c.moveToFirst();
StringBuffer str = new StringBuffer();
str.append("DBversion:::::"+c.getDatabase().getVersion()+"\n");
while(c.moveToNext()){
Log.i("SQLCipher", "Cipher cursor data"+c.getString(1) );
str.append(c.getString(1)+"\n");
}
Toast.makeText(SqlCipherSampleAppActivity.this, str.toString(), Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Log.i("SQLCipher", "sqlcipher show exception====:::"+e.toString());
Toast.makeText(SqlCipherSampleAppActivity.this, "Exception while reading data+\n"+e.toString(), Toast.LENGTH_LONG).show();
}
}

SQLiteCursor c =(SQLiteCursor) db.getDBData();  line gives java.lang.ClassCastException: net.sqlcipher.CrossProcessCursorWrapper

I need to cast to SQLiteCusror to use some of the APIS from  SQLiteCusror   class.

Plz help in resolving this problem..
SqlCipherSampleApp.zip

Nick Parker

unread,
Jul 25, 2012, 9:08:16 AM7/25/12
to sqlc...@googlegroups.com, ashiq sayyad
Hi Ashiq,

Based on the example you have provided, the SQLiteCursor is not needed for the data you are gathering.  The calls to getCount and getString are available from the Cursor interface returned from your query and the version is available directly from your SQLiteDatabase reference.  The CrossProcessCursorWrapper is needed to provide cross process cursors in SQLCipher for Android.
-- 
Nick Parker

Nick Parker

unread,
Jul 25, 2012, 12:16:05 PM7/25/12
to ashiq sayyad, sqlc...@googlegroups.com
Hi Ashiq,

Generally speaking, if your code were written against the interface you would not have to change much, as that tends to be a best practice.  The class cast exception occurs because we are not giving you back a SQLiteCursor, but instead a CrossProcessCursorWrapper.  Note that both implement the Cursor interface.  You should adjust your implementation to only rely on the Cursor interface.

On 7/25/12 8:57 AM, ashiq sayyad wrote:
Thanks Nick ..
 
But can u tell me why classcast exception is happening only with sqlcipher jars .The code works perfectly fine if I use the same casting with android sdk database apis.
  
Because as per the sqlcipher site, we just need to change the import statements to switch from android database to sqlcipher database.No need to change the code base except some of the methods which require password as argument...

One more thing,the sample I shared can be made working with some workarounds. But in case I want to use some SqLiteCursor  specific APIS (which are not there in cursor), I need to do type casting.

-- 
Nick Parker
Reply all
Reply to author
Forward
0 new messages