Android 11 Open database failed. Could not open database on some Android devices · Issue #462 · andpor/react-native-sqlite-storage · GitHub
Skip to content

Android 11 Open database failed. Could not open database on some Android devices #462

Open

Description

@TheGameNewWorldOrder

Hi,

Some of our users have reported an issue related to the SQLite lib. After updating their phone's OS( Color OS) to 11(Android 11), SQLite operation is broken. In debug mode I see below error:

OPEN database: StorageExtension.db failed, aborting any pending transactions
and following if trying to call any transaction:

error: openDatabase {"dblocation": "nosync", "location": "default", "name": "StorageExtension.db"} [[Error: Could not open database]]

I have verified the app and plugin work fine on Color OS 7 and 10, but broken on 11.

Expected Behavior

DB connection is as usual and DB can be opened.

Current Behavior

Opening DB failed

Possible Solution

Related to Android 11?

Steps to Reproduce (for bugs)

Example code im using:

`

SQLite.enablePromise(true);
SQLite.DEBUG(true);

const init = async () => {
await SQLite.echoTest();
db = await SQLite.openDatabase({
name: DATABASE_NAME,
location: 'default',
});
await createTables(db);
};
`

Note that this code works fine on older Color OS and other Android phone like Samsung and Huawei

Your Environment

  • React Native SQLite Storage Version used: "5.0.0"
  • React Native version used: "0.63.3"
  • Operating System and version (simulator or device): Oppo Color OS 11, Android 11
  • IDE used: Android Studio 3.5

Debug logs

image

As shown above, the db was created but at /data/data/<package_path>/databases/

I have also tried the solution to create pre-populated DB but with no luck: https://medium.com/infinitbility/react-native-sqlite-storage-422503634dd2

Any idea why this is happening? We also have users on OnePlus and Pixel reporting the same problem.

Activity

  1. TheGameNewWorldOrder commented on Jan 13, 2021

    @TheGameNewWorldOrder
    Author

    after looking into the Android code of the plugin, i found that the operation failed at long var1 = SQLiteNative.sqlc_db_open(this.dbfilename, this.openflags); which returns negative value on Android 11 while returning 0 on lower versions. Possible related reports in other sqlite implementation: brody2consult/cordova-sqlite-storage#954 (comment)

  2. kinor commented on Jan 25, 2021

    @kinor

    Hi. Has there been any movement in resolving this problem?
    Thanks

  3. andpor commented on Jan 25, 2021

    @andpor
    Owner

    @kinor not able to allocate time to this but all PR (backward compatible and also covering Android native) are welcome.

  4. bekkii77 commented on Jan 27, 2021

    @bekkii77

    I'm experiencing similar issues.
    batch execution error: database not open after opening the database and receiving confirmation it's open
    and
    database already open: <databasename>
    These are only appearing in Android 11, Android 10 is fine.

  5. LeeKyungJoon commented on Mar 19, 2021

    @LeeKyungJoon

    I run well on Android 11.
    Looking at the uploaded code, createFromLocation is missing.
    I think it should be necessary. Add it and try it out.

  6. TheGameNewWorldOrder commented on Jun 9, 2021

    @TheGameNewWorldOrder
    Author

    @LeeKyungJoon , this is not a pre-populated database. This issue happens on specific OS/Devices only. What device/OS did you use? My testing on huawei and Samsung were all fine, but Oppo/ColorOS fails.

  7. TheGameNewWorldOrder commented on Jun 9, 2021

    @TheGameNewWorldOrder
    Author

    If your target SDK is 29, it runs fine on Android 11, but since Google forces all app updates to target 30 or above, this is no longer an option. We have 4 month left to do the update.

  8. KarenMuller commented on Jul 13, 2021

    @KarenMuller

    Hi, Did you find a solution for this? I know public external storage changed from Android 11. But I am not sure where to put my sqlite database and how to access this. Db must not be deleted if app is uninstalled. Other apps must also be able access this sqlite db.

  9. AmolFC commented on Aug 30, 2021

    @AmolFC

    Any update on this? App is not working on many devices with Android 11 now.

  10. himu243 commented on Sep 2, 2021

    @himu243

    Facing the Similar issue. It is showing the following error on my Samsung Galaxy M51 device.

    Android Version: 11
    OneUI Version: 3.1

    LOG new transaction is waiting for open operation
    LOG OPEN database: master.db failed, aborting any pending transactions
    LOG error: openDatabase {"dblocation": "nosync", "name": "master.db"} [[Error: Could not open database]]

    P.S. This is only happening when i make the targetSDKVersion 30. It is working fine on targetSDKVersion 29.

    Can someone please help on this one?

    Thanks in Advance

  11. zzorba commented on Oct 8, 2021

    @zzorba

    Seeing this issue as well now, once I started targeting SDK Version 30 (RN65+).

    I am not an Android expert, but I believe it is something related to this article (https://developer.android.com/about/versions/11/privacy/storage#permissions-target-11), which details that once you target SDK 30, you now must use the modern permissions system for accessing disks (on all versions). I was using the android-native version, which might be part of the problem here.

  12. intentzero commented on Oct 8, 2021

    @intentzero

    Facing a similar issue as well.
    Tested on Android 9.0 API 28, Android 10.0 API 29 and Android 11 API 30.

    My use case is as follows:
    Download a zipped db file from remote server into a cache directory, unzip it and place it into a Documents folder.
    This workflow works well with "expo-sqlite" library, I can sucessfully download, unzip and load the db.

    While trying to reproduce it with the "react-native-sqlite-storage" I'm getting the "Error: Could not open database" issue.
    I'm using the following command for opening the database:

    SQLite.openDatabase({
      name: 'mydatabase.sqlite',
      createFromLocation: '/data/user/0/com.myapp/files/SQLite/mydatabase.sqlite',
      location: 'default'
    })

    Also, I've confirmed that the "mydatabase.sqlite" file does indeed exist on the "/data/user/0/com.myapp/files/SQLite/" folder.

    However, if I manually place the "mydatabase.sqlite" file directly into my project android asset's folder ("android/app/src/main/assets") and change the opening command attribute "createFromLocation" to "~mydatabase.sqlite", the database loads!

    I've noticed that I'm using an absolute path for the first example, while on the second I'm using a relative path... Could it be a reason for the issue I'm seeing?

    If someone could shed some light on this, I would be very gratefull...

    Many thanks in advance!

  13. rblalock commented on Oct 12, 2021

    @rblalock

    I am running into an identical problem.

    Related to this and PR #492 .... curiously enough ... I removed the custom SQLite build and used the device's SQLite and I got it to work with my existing code.

    i.e. I removed this from the react-native.config.js and it worked.

        "react-native-sqlite-storage": {
          platforms: {
            android: {
              sourceDir:
                "../node_modules/react-native-sqlite-storage/platforms/android-native",
              packageImportPath: "import io.liteglue.SQLitePluginPackage;",
              packageInstance: "new SQLitePluginPackage()"
            }
          }
        }
    
  14. himu243 commented on Oct 20, 2021

    @himu243

    @rblalock It worked for me as well but not sure how is it working. Can you please explain that as well?

  15. rblalock commented on Oct 20, 2021

    @rblalock

    I assume it has to do with the drivers for SQLite but I'm not 100% sure

  16. 32 remaining items

  17. castalonirenz commented on Apr 30, 2022

    @castalonirenz

    Due to maintainer inactivity to accept PRs I've created another package based on this package with new android native libraries for API 30 available to use:
    https://www.npmjs.com/package/react-native-sqlite-storage-api30 https://github.com/Ehsan0Hejazi/react-native-sqlite-storage-api30
    npm i react-native-sqlite-storage-api30

    Can you tell us what are the changes you have done here so we can have an idea? thanks in advance

    I have added new android native libraries patches which is under /platforms/android-native/libs other parts are remained the same,

    so now you can switch your android target API to 30 and there will be no bugs and errors

    The app is crashing on apk release

  18. Ehsan0Hejazi commented on Apr 30, 2022

    @Ehsan0Hejazi

    Due to maintainer inactivity to accept PRs I've created another package based on this package with new android native libraries for API 30 available to use:
    https://www.npmjs.com/package/react-native-sqlite-storage-api30 https://github.com/Ehsan0Hejazi/react-native-sqlite-storage-api30
    npm i react-native-sqlite-storage-api30

    Can you tell us what are the changes you have done here so we can have an idea? thanks in advance

    I have added new android native libraries patches which is under /platforms/android-native/libs other parts are remained the same,
    so now you can switch your android target API to 30 and there will be no bugs and errors

    The app is crashing on apk release

    Please create an issue with more details under the package github so we can track it

  19. castalonirenz commented on Apr 30, 2022

    @castalonirenz

    thanks

  20. castalonirenz commented on Apr 30, 2022

    @castalonirenz

    Due to maintainer inactivity to accept PRs I've created another package based on this package with new android native libraries for API 30 available to use:
    https://www.npmjs.com/package/react-native-sqlite-storage-api30 https://github.com/Ehsan0Hejazi/react-native-sqlite-storage-api30
    npm i react-native-sqlite-storage-api30

    Can you tell us what are the changes you have done here so we can have an idea? thanks in advance

    I have added new android native libraries patches which is under /platforms/android-native/libs other parts are remained the same,
    so now you can switch your android target API to 30 and there will be no bugs and errors

    The app is crashing on apk release

    Please create an issue with more details under the package github so we can track it

    image

    Here is the logcat from apk release build

  21. Ehsan0Hejazi commented on Apr 30, 2022

    @Ehsan0Hejazi

    ok I think I found the problem
    just follow this

    in react-native.config.js

    your dependencies should be as follows

    dependencies: {
          "react-native-sqlite-storage-api30": {
            platforms: {
              android: {
                sourceDir:
                  "../node_modules/react-native-sqlite-storage-api30/platforms/android-native",
                packageImportPath: "import io.liteglue.SQLitePluginPackage;",
                packageInstance: "new SQLitePluginPackage()"
              }
            }
          }
        }
    
    

    you are exporting "react-native-sqlite-storage" while you need to export "react-native-sqlite-storage-api30"

    let me know if your problem is solved

  22. amirkhanzada51 commented on May 3, 2022

    @amirkhanzada51

    I am running into an identical problem.

    Related to this and PR #492 .... curiously enough ... I removed the custom SQLite build and used the device's SQLite and I got it to work with my existing code.

    i.e. I removed this from the react-native.config.js and it worked.

        "react-native-sqlite-storage": {
          platforms: {
            android: {
              sourceDir:
                "../node_modules/react-native-sqlite-storage/platforms/android-native",
              packageImportPath: "import io.liteglue.SQLitePluginPackage;",
              packageInstance: "new SQLitePluginPackage()"
            }
          }
        }
    

    Hey, you save my soul. after 2 days of headache finally, solve the problem.

  23. Raj797979 commented on Oct 10, 2022

    @Raj797979

    can you guide me cause i'm getting same issue with android version 11 and above

  24. usmanmalik333 commented on Jan 30, 2023

    @usmanmalik333

    still not working on android 11 any solution ? if we removed the dependencies then it not working on other android version

  25. amf-paulinho commented on Feb 6, 2023

    @amf-paulinho

    I am having same problem. Is this still open?

    if I want to use SQLite that comes with the library Android 11 cant open the database, If I use the Device SQLite lots of Android 10 will crash.

    this is my config file:

    module.exports = {
      dependencies: {
        'react-native-sqlite-storage': {
          platforms: {
            ios: {},
            android: {
              sourceDir: '../node_modules/react-native-sqlite-storage/platforms/android-native',
              packageImportPath: 'import io.liteglue.SQLitePluginPackage;',
              packageInstance: 'new SQLitePluginPackage()',
            },
          },
        },
      },
    };
    
    

    Removing it solves for Android 11 however android 10 will not work.
    Device I am testing are Motorola One 5G Ace - Android 11 and Galaxy S9+ Android 10 (also another motorola One using Android 10 )

    There is no solution still ?

  26. shiroze commented on Mar 1, 2023

    @shiroze

    I am running into an identical problem.

    Related to this and PR #492 .... curiously enough ... I removed the custom SQLite build and used the device's SQLite and I got it to work with my existing code.

    i.e. I removed this from the react-native.config.js and it worked.

        "react-native-sqlite-storage": {
          platforms: {
            android: {
              sourceDir:
                "../node_modules/react-native-sqlite-storage/platforms/android-native",
              packageImportPath: "import io.liteglue.SQLitePluginPackage;",
              packageInstance: "new SQLitePluginPackage()"
            }
          }
        }
    

    Not working on Android 11+ on Device Redmi Note 9 Pro

  27. LaGregance commented on Jun 26, 2023

    @LaGregance

    Due to maintainer inactivity to accept PRs I've created another package based on this package with new android native libraries for API 30 available to use:

    https://www.npmjs.com/package/react-native-sqlite-storage-api30 https://github.com/Ehsan0Hejazi/react-native-sqlite-storage-api30

    npm i react-native-sqlite-storage-api30

    Thanks a lot, it's work for me, just note in order to not break others package that are based on it, you can install the package like that:

    yarn add react-native-sqlite-storage@npm:react-native-sqlite-storage-api30
    

    So you can refer to it using standard import XXX from 'react-native-sqlite-storage'

  28. hengkx commented on Aug 21, 2023

    @hengkx

    Do you have any solutions?

  29. Mohammed-Aljasem commented on May 13, 2024

    @Mohammed-Aljasem

    any updates ?

  30. LaGregance commented on Jun 21, 2024

    @LaGregance

    Hi,

    I've created a fork of this repository that bring multiple things:

    • Fix this issue
    • Add UUID support
    • Update SQLite version to 3.43.1 (release on 2023-09-11)
    • Bring same version of SQLite for iOS & Android, independent of OS version (this mean SQLite is also compiled for iOS)

    The fork is a drop in remplacement of this repository.

    To see it, click here: https://www.npmjs.com/package/@boltcode/react-native-sqlite-storage

    If you want to understand what I've done in this fork, there is a documentation about it.

    Have a good day.

  31. antoinerousseau commented on Dec 23, 2024

    @antoinerousseau

    Still having the issue on Android 14/15
    boltcode-js/react-native-sqlite-storage#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions