Trey Balut
Trey Balut

Reputation: 1395

UWP PreLoaded SQLite

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.

Upvotes: 1

Views: 104

Answers (2)

Xie Steven
Xie Steven

Reputation: 8601

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); 
}

Upvotes: 0

Mamoru Satoh
Mamoru Satoh

Reputation: 2710

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

Upvotes: 1

Related Questions