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

Most upvoted comments

SOLVED: How to use pre-populated database with react-native-windows

Step 1: Link files

  1. Put your database in windows\<ProjectName>\www
  2. Open windows\<ProjectName>.sln with Visual Studio
  3. Right-click <ProjectName>\Assets in the Solution Explorer
  4. Add -> Existing Item and select your database
  5. On the right-hand side, make sure to set Content to True

Step 2: Fix SQLitePlugin error

  1. Still in Visual Studio, in the Solution for SQLitePlugin, open SQLitePlugin.cpp
  2. Find the line that says CopyDbAsync(assetFile, to_hstring(*dbFileName)).GetResults();
  3. Replace that entire try/catch with
try
{
    CopyDbAsync(assetFile, to_hstring(*dbFileName)).get();
}
catch (hresult_error const& ex)
{
    if (ex.code() == 0x800700B7) 
    {
        // Ignore, CopyDbAsync throws when the file already exists.
    } else
    {
        onFailure("Unable to copy asset file: " + winrt::to_string(ex.message()));
        return;
    }
}

You should now be able to use createFromLocation = '1' the same as in Android and iOS

Logic 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.