react-native-sqlite-storage: Unable to get createFromLocation option working for Windows
I’m trying to open an existing database using the createFromLocation option, but I just can’t get it to work. I’ve tried
openDatabase({ name: "test", location: 'default', createFromLocation: "1" },
() => { console.log("db load success!") }, (e) => { console.error("db load failure!"); console.error(e); console.error(e.code) });
which results in the error message being written. The error printed was “Error: Could not open database” followed by a stack trace. I have saved my sqlite file in D:\appname\windows\appname\www
and have updated the visual studio solution to “Include In Project” the db file.
I have also tried
openDatabase({ name: "test", location: 'default', createFromLocation: "~Assets/test.sqlite" },
() => { console.log("db load success!") }, (e) => { console.error("db load failure!"); console.error(e); console.error(e.code) });
which gives the same error.
I’ve made sure to follow the 4 windows specific setup steps listed on the repo README.
For what its worth, I’ve been compiling my app only through npx react-native run-windows
.
Have I missed a step somewhere?
Expected Behavior
I’d expect the database file to be read.
Current Behavior
It’s unable to read the file.
Possible Solution
Steps to Reproduce (for bugs)
Try createFromLocation option with React Native Windows.
Context
Your Environment
- React Native SQLite Storage Version used: 3
- React Native version used: react-native: 0.66.0-rc.1 => 0.66.0-rc.1 react-native-windows: 0.66.0-preview.6 => 0.66.0-preview.6
- Operating System and version (simulator or device): Windows 10 10.0.19042
- IDE used: Visual Studio
- Link to your project:
Debug logs
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 15
SOLVED: How to use pre-populated database with
react-native-windows
Step 1: Link files
windows\<ProjectName>\www
windows\<ProjectName>.sln
with Visual Studio<ProjectName>\Assets
in the Solution ExplorerAdd -> Existing Item
and select your databaseContent
toTrue
Step 2: Fix SQLitePlugin error
SQLitePlugin.cpp
CopyDbAsync(assetFile, to_hstring(*dbFileName)).GetResults();
try/catch
withYou should now be able to use
createFromLocation = '1'
the same as in Android and iOSLogic explained here.
I’ve long since given up on this but keep it up Captain Ahab!
For the next soul who ends up here:
On Windows the default location ends up in C:\Users<USER NAME>\AppData\Local\Packages<RANDOM STRING>
However, “createFromLocation:1” doesn’t seem to save the database at all anywhere (I just searched my entire drive for it and it’s not showing up). I’m not sure how that’s possible, since you can still write to it in the program. I’ll keep looking into this.
@andpor Do you have any idea where it expects the file to be? I’ve tried every location and combination I can think of with no luck.