Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

'Database is locked'

When I ran my app after creating my database, I got this error:

04-10 11:39:22.783 2233-2233/com.example.x.x E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.x.x, PID: 2233
                                                                          android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
                                                                              at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                              at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
                                                                              at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:638)
                                                                              at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:319)
                                                                              at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:293)
                                                                              at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:214)
                                                                              at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:192)
                                                                              at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
                                                                              at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
                                                                              at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
                                                                              at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:916)
                                                                              at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:893)
                                                                              at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:796)
                                                                              at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
                                                                              at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
                                                                              at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                              at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                              at com.example.diego.easylist.EasyListDataSource.open(EasyListDataSource.java:25)
                                                                              at com.example.diego.easylist.EasyListDataSource.create(EasyListDataSource.java:75)
                                                                              at com.example.diego.easylist.Activitys.ListsActivity.saveList(ListsActivity.java:271)
                                                                              at com.example.diego.easylist.Activitys.ListsActivity.createListClickListener(ListsActivity.java:197)
                                                                              at com.example.diego.easylist.Fragments.NewListDialogFragment$1.onClick(NewListDialogFragment.java:92)
                                                                              at android.view.View.performClick(View.java:5217)
                                                                              at android.view.View$PerformClick.run(View.java:21349)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5585)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

I'm not sure what the problem here is. Here is my code: EasyListDataSource.java25:

    private SQLiteDatabase open() {
        return mEasyListSQLiteHelper.getWritableDatabase();
    }

EasyListDataSource.java75

     public void create(ListItem item) {
        SQLiteDatabase database = open();
        database.beginTransaction();

        ContentValues contentValues = new ContentValues();
        contentValues.put(EasyListSQLiteHelper.LISTS_NAME, item.getListName());
        contentValues.put(EasyListSQLiteHelper.LISTS_CHECKED, item.getImportant());
        database.insert(EasyListSQLiteHelper.LISTS_TABLE, null, contentValues);

        database.setTransactionSuccessful();
        database.close();
        close(database);
    }

ListsActivity.java271:

    private void saveList() {
        EasyListDataSource easyListDataSource = new EasyListDataSource(this);
        easyListDataSource.create(mListItem);
    }

ListsActivity.java197:

    @Override
    public void createListClickListener(View v, String string) {
        mListItem = new ListItem();
        mListItem.setListName(string);
        mListList.add(mListItem);
        mRecyclerViewLists.scrollToPosition(mListList.size() - 1);
        mAdapter.notifyDataSetChanged();
        saveList();
        Toast.makeText(ListsActivity.this, "\"" + mListItem.getListName() + "\"" + " has been created.", Toast.LENGTH_SHORT).show();
    }

NewListDialogFragment.java92:

        // OnClickListeners...
        mAcceptListTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String item = mListEditText.getText().toString();
                if (item.equals("") || item.startsWith(" ")) {
                    String message = "Please fill in the empty field.";
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                } else {
                    mCreateListClickListener.createListClickListener(view, item);
                    dialog.dismiss();
                }
            }
        });

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Have you tried googling the error message, specifically this most important part: "SQLiteDatabaseLockedException: database is locked"? This error usually means that something else in your app has an open connection to the DB, preventing dual access. There are some helpful suggestions on StackOverflow to try to debug further.