Andrii Chernysh
Andrii Chernysh

Reputation: 375

Cannot get writable database. SQLiteException: not an error (code 0)

When I try to get writable (and readable) database with SQLiteOpenHelper I receive error :

 E/SQLiteDatabase: Failed to open database '/data/user/0/com.smt/databases/SMT'.
         android.database.sqlite.SQLiteException: not an error (code 0)
         at android.database.sqlite.SQLiteConnection.nativeRegisterLocalizedCollators(Native Method)
         at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:361)
          at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
         at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
          at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
         at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:835)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:820)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:723)
        at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:669)
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:290)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at com.smt.model.data.db.DatabaseHelper.<init>(DatabaseHelper.java:48)
        at com.smt.model.data.db.SmtDatabase.<init>(SmtDatabase.java:48)
        at com.smt.presenter.impl.CatalogPresenterImpl.loadCatalogForParentId(CatalogPresenterImpl.java:40)
        at com.smt.view.fragment.CatalogFragment.onCreateView(CatalogFragment.java:61)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
      at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1322)
      at android.app.Activity.performStart(Activity.java:6777)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2819)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6321)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Here is my SQLiteOpenHelper

DatabaseHelper.java:

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_CATEGORIES);
        db.execSQL(SQL_CREATE_MATERIALS);
        db.execSQL(SQL_CREATE_LOCALIZATIONS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DROP_CATEGORIES);
        db.execSQL(SQL_DROP_LOCALIZATIONS);
        db.execSQL(SQL_DROP_MATERIALS);

        onCreate(db);
    }
}

Methods onCreate or onUpgrade are not calling.

SmtDatabase.java:

public class SmtDatabase {
    private static final String TAG = SmtDatabase.class.getName();

    private Context mContext;
    private SQLiteDatabase mDatabase;

    public SmtDatabase(Context context) {
        DatabaseHelper dbHelper = new DatabaseHelper(context);
        mDatabase = dbHelper.getWritableDatabase(); // <------  Here is error
        this.mContext = context;
    }

    /* SELECT, QUERY, UPDATE Methods */
}

My manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.smt">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <!-- After there are application with activity definitions -->
</manifest>

Also I request read and write external storage at runtime. Tested on device with android 7.1.1 and emulator with android 6.0. Both gets error. But emulator with android 4.4 works good!!!

I can't understand what I am doing wrong. Yesterday this code worked like a charm, but today I always get "Not an error SQLite exception".

Upvotes: 4

Views: 1517

Answers (1)

Andrii Chernysh
Andrii Chernysh

Reputation: 375

For 2 days I tried to fix this issue, but it was no result. Today I have created new project, then copy/paste all classes and it was a cure.

UDP: I have found why this issue appeared. When I changed language with code below in my Application.java class I put incorrect string value ("русский"), configuration became unavailable to create database correctly.

@Override
public void onCreate() {
    super.onCreate();
    Fabric.with(this, new Crashlytics());
    PreferenceManager mPreferenceManager = new PreferenceManager(getApplicationContext());

    mLang = mPreferenceManager.getLocale();  // Here I have got incorrect value

    if (mLang.equals("default")) mLang = getResources().getConfiguration().locale.getLanguage();

    Log.d(TAG, "onCreate: lang = " + mLang);

    mLocale = new Locale(mLang);
    Locale.setDefault(mLocale);
    Configuration config = new Configuration();
    config.locale = mLocale;
    getBaseContext().getResources().updateConfiguration(config, null);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mLocale = new Locale(mLang);
    Locale.setDefault(mLocale);
    Configuration config = new Configuration();
    config.locale = mLocale;
    getBaseContext().getResources().updateConfiguration(config, null);
}

Upvotes: 6

Related Questions