Retrieving BLOB from SQLite database android

萝らか妹 提交于 2019-12-05 21:24:01
JoxTraex

Did you verify that there is actually data in your cursor before attempting to retrieve anything?

if(c.getCount() > 0){do something}

if you Don't do this then you will be attempting to retrieve something that is already null which is a BIG NO NO

Also when doing your get of the elements it NOT good practice to retrieve like this: c.getBlob(0)

Other info

The reason for this is because if you decide to change your columns and change the order, this will heavily bite you.

My suggestion is to have static variables that exist in a helper class that reference the column names you have defined such like this:

c.getString(c.getColumnIndex(String columnName))

This is far better practice as you can easily change the database by just referencing the name.

Read my post here on how to handle a database and make indirect calls to your database. You will be thankful of all the headaches you will avoid.

Check here: Storing Lists to A Database, and Retrieving Them All Together : Android

Also when posting a question, a logcat of the error helps solve the problem faster.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!