Solution to android.database.sqlite.sqliteexception code 5 database is locked

May 24, 2012 | Rishabh Agrawal | General

Error at runtime: caused by android.database.sqlite.sqliteexception error code 5 database is locked
I have encountered the above mentioned error, when I am calling asyncTask  constructor from my activity again and again for saving the image in external database. In the asyncTask, I am opening the  database and after doing some stuff, I am closing the database.Parallely, I am  running a service from activity which is also doing some database work.
My Code is like this:

Class MyActivity extends Activity{
CommunicationWithDBCls communic=new CommunicationWithDBCls();

call(){

imgAsynTask = new ImageShowAsyncTask(this, imgUrlAryLi, count,
                    this, progress);
            imgAsynTask.execute();
}
}

And

public class ImageShowAsyncTask extends AsyncTask<Void, String, Bitmap> {
CommunicationWithDBCls communic=new CommunicationWithDBCls(); 
public ImageShowAsyncTask(CallBackInterFace callBackInterface,
            ArrayList<String> arryLisUrl, int count, Context cont,
            ProgressBar progress) {

// some stuff
}

@Override
    protected Bitmap doInBackground(Void… paramArrayOfParams) {

try{
//some stuff related to getting image from server n saving it to DB
}catch(Exception e){

}finally{
communic.close();
}

}

}

Sometimes, the code is giving the error as:
caused by android.database.sqlite.sqliteexception error code 5 database is locked
I broke my head in getting rid of this problem but finally I solved it. 🙂
I got rid off the above problem when I did a little change in my code. Instead of opening and closing the database
inside the asyncTask, I just passed the reference of my database class while calling the constructor of asyncTask from
the activity.

the correct Code is like this:

Class MyActivity extends Activity{
CommunicationWithDBCls communic=new CommunicationWithDBCls(); 
call(){

imgAsynTask = new ImageShowAsyncTask(this, imgUrlAryLi, count,
                    this, progress,communic);
            imgAsynTask.execute();
}
}

And

public class ImageShowAsyncTask extends AsyncTask<Void, String, Bitmap> {

CommunicationWithDBCls communic;
public ImageShowAsyncTask(CallBackInterFace callBackInterface,
            ArrayList<String> arryLisUrl, int count, Context cont,
            ProgressBar progress, CommunicationWithDBCls communic2) {

//some stuff
}

@Override
    protected Bitmap doInBackground(Void… paramArrayOfParams) {

try{
//some stuff related to getting image from server n saving it to DB
}catch(Exception e){

}

}

}

Now, it’s working good and I am enjoying with my work 🙂 …
Hope it will work for others too…
-By Arvind Kaushal
Android Developer

THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal