Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new transaction is waiting for open operation on Android #184

Closed
arnsa opened this issue Jul 21, 2017 · 3 comments
Closed

new transaction is waiting for open operation on Android #184

arnsa opened this issue Jul 21, 2017 · 3 comments

Comments

@arnsa
Copy link

arnsa commented Jul 21, 2017

So I've been trying to open up an sqlite database. Everything worked perfectly fine on iOS, but the problem is with Android. It seems that it can't find database file anyhow. I've tried probably 100 different ways and none of them worked. All I need to do is to just read data from the database. There's no need for me to write any data to database. The thing is that I can't really put database sqlite file in the assets folder, because database file is being downloaded over the internet when the app is launched. I am using react-native-fs package to download db file. I then save it to "RNFS.ExternalDirectoryPath/maps" folder.

From my understanding, this is how the DB should be opened on Android:

SQLite.openDatabase({ name: "dbname.sqlite", createFromLocation: "/data/com.app_bundle_name/files/maps/dbname.sqlite", readOnly: true });

But whenever I'm trying to run executeSql method, I get this: "new transaction is waiting for open operation". I've tried running a debugger and it seems that it can't find this file. Please, whoever has any ideas, help me out. I am out of ideas what to do. Thank you!

@hitgeek
Copy link

hitgeek commented Jul 25, 2017

I ran into similar issues (on Android only) when trying to access a database downloaded at application start. I hit 3 main gotchas.

  1. the database "name" is probably not the filename. My sqlite database had the default name of "main". You can check the name using the .databases in the sqlite3 command line client.

  2. I was downloading the database to RNFS.DocumentDirectory which on Android is "files". I think RNFS puts everything here on Android (I see your path contains "files"). To access this I just needed to use createFromLocation: 'db.sqlite', no /data, or absolute paths. In your case I think it would be createFromLocation: 'maps/dbname.sqlite'

  3. readyOnly did not work. When I debugged in Android Studio I saw exceptions related to "can't write to read only database." Not sure why since I was only doing select queries (my guess is something to do with the way it wraps everything in transactions), but removing readOnly fixed the problem.

so what evetually worked for me was

var opts = Platform.OS == "ios" ? {name: 'db.sqlite', location: 'Documents'} :
                                    {name: 'main', createFromLocation : "db.sqlite"}

SQLite.openDatabase(opts)

pretty weird that the parameters are so different between the 2 platforms

@arnsa
Copy link
Author

arnsa commented Jul 25, 2017

@hitgeek thank you for this detailed answer. Issue fixed!

@arnsa arnsa closed this as completed Jul 25, 2017
@vmarquet
Copy link

I had the same problem on Android. The createFromLocation parameter was responsible for messing things up. Using only the name parameter is sufficient on both platforms.

This is what I'm using, on both Android and iOS:

SQLite.openDatabase({ name: "dbname.sqlite"});

On Android 7.0, the database will be saved to /data/data/com.mypackage/databases/dbname.sqlite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants