1
votes

I am trying to copy a preloaded SQLite db into my UWP app. On the initial installation it copies the "test.db", but the size is 0 bytes and there are no tables or data. The original db is 1300 bytes with data and tables.

Another factoid...when I create the app Using Visual Studio 2017 and compile and run/debug the app it works fine, but when I sideload the appx file or download from the Windows Store the db is empty.

Here is the code that I am using:

  Task task = CopyDatabase();


 private async Task CopyDatabase()
    {
        bool isDatabaseExisting = false;

        try
        {
            StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Express.db");
            isDatabaseExisting = true;
        }
        catch
        {
            isDatabaseExisting = false;
        }

        if (!isDatabaseExisting)
        {
            StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("Express.db");
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, "Express.db", NameCollisionOption.ReplaceExisting);
        }
    }

I'm not getting any error messages.

2
I can't answer off the top of my head but some things to try: After assigning "databaseFile" create a StorageFile options object and check the file size during runtime using a break point. See it's 0 or 1300 bytes. Also instead of copying it to the LocalFolder, try creating a subfolder inside LocalFolder and copying it there. - Sean O'Neil
When did you call the CopyDatabase method? In "App.xaml.cs" OnLaunched handler? - Xie Steven
I call "CopyDatabase" in public MainPage() public MainPage() { gui = this; InitializeComponent(); Task task = CopyDatabase(); DataSetup(); CreateNewChartButton.Visibility = Visibility.Collapsed; SignInButton_Click(null, null); } - Trey Balut

2 Answers

1
votes

Does the your database file deployed correctly to the target system? To confirm it, see your deployed - "Package" - folder. Open command prompt with administrative previleges, and see the directory

c:\Program Files\WindowsApps\your-app-id

If your database file deployed successfully, you can see it in the directory. If not, you may need to change the deploy settings.

To deploy the file to target machine, you should set the property of the one as ...

'BuildAction=Contents'

'Copy to output directory'='Always Copy'

You can set it from solution explorer and right-click the your database file.

If you succeeded the deploying file, your code will copy your database file to app local folder.

c:\Users\YOUR-USER-ID\AppData\Local\Packages\YOUR-APP-ID\LocalState

0
votes

First, you would need to use await for your CopyDatabase method.

Second, I suggest you call this method in MainPage_Loaded event handler instead of MainPage's Constructor.

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    gui = this; InitializeComponent(); 
    await CopyDatabase(); 
    DataSetup(); 
    CreateNewChartButton.Visibility = Visibility.Collapsed; 
    SignInButton_Click(null, null); 
}