Answered by:
How to copy a SQLite Db file in Document Folder into App Local Folder

Question
-
Say, I have a pre-populated SQLite DB file in Document Folder (Known folder) and I wan to import this file into my App's LocalFolder. Is this allowed? Can you show me the code how this is done. Your help is appreciated.
Thanks
Monday, February 17, 2014 8:21 AM
Answers
-
You don't need side-loading to use the FileOpenPicker to pick from the Document folder. This is the recommended and generally best way.
A side-loaded app can also use the DocumentsLibrary capability to access documents folder without the user's direct permission, but your usage here is not covered by the certification requirements.
--Rob
- Marked as answer by FireDance Tuesday, February 18, 2014 5:05 AM
Tuesday, February 18, 2014 1:37 AMModerator
All replies
-
Hi FireDance,
Take a look at this post: can't get database file to copy to local folder
The solution has been provided by jrboddie Hope this helps.
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Edited by Jamles HezModerator Monday, February 17, 2014 1:27 PM
Monday, February 17, 2014 1:27 PMModerator -
Hi Jamles
Thanks for the link. The database is in the same installed folder as the App and copy it to the Local Folder.
However, as for my case, my SQLite database is in Document Folder and I want to copy it into LocalFolder. I am not sure what are the requirements needed to perform such operation. Thus need help.
Monday, February 17, 2014 1:34 PM -
Hi ,
I believe that the file sample will help you on this
http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597
Since you are using Sqlite , getting the db and copying to the App's local folder all you have to do afterwards is to point to the new location.
Here's some code . I used it to keep a backup of my database
private StorageFolder m_downloadfolder;
public StorageFolder Download_Folder
{
get { return m_downloadfolder; }
set { m_downloadfolder = value; }
}public async void KeepBackup()
{
await KeepTheBackup();
}
private async Task KeepTheBackup()
{
Boolean searchResult = false;
StorageFolder storageFolder = KnownFolders.MusicLibrary; //Let's say that you have in there the dbtry
{
//Get your file
StorageFile sampleFile = await storageFolder.GetFileAsync("mydatabase.db");
searchResult = true;
}
catch (Exception)
{
searchResult = false;
}
if (searchResult == true)
{ //found the database so proceed with the copy --------
Download_Folder = ApplicationData.Current.LocalFolder // it returns your app's local folder.This is your destination folder
await sampleFile.CopyAsync(Download_Folder);
sampleFile = await Download_Folder.GetFileAsync("MyDatabase.db");
//check now if the sampleFile is not null and if the copy succeeded
}
}
now since that your app finishes the copy connect to the new path
//create a property and put it in your App.Xaml.cs . FYI whatever you put in there you can have globally in your app
private string m_dbpath;
public string dbpath
{
get { return m_dbpath; }
set { m_dbpath = value; }
}public SQLite.SQLiteConnection MyConnection()
{this.dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "MyDatabase.db");
var db = new SQLite.SQLiteConnection(this.dbpath);//the db path points now to your new local folder.
return db;
}Now whenever you call the above it returns you the db connection
Hope I helped you
thank you
Monday, February 17, 2014 1:56 PM -
The app cannot access documents directly. Use a FileOpenPicker to let the user choose the SQLite database file then call the returned StorageFile's CopyAsync method to copy it to the desired app data directory.
You can use JRBoddie's code except changing how you get the original SQLite file to use the picker.
--Rob
Monday, February 17, 2014 3:48 PMModerator -
Thank you all. I have one more question.
This LOB app is for Side-loading and it will not be uploaded to App store. With this nature of side loading, can this app still be able to access Document Folder using FileOpenPicker or code. I need this confirmation.
Thanks
Tuesday, February 18, 2014 1:22 AM -
You don't need side-loading to use the FileOpenPicker to pick from the Document folder. This is the recommended and generally best way.
A side-loaded app can also use the DocumentsLibrary capability to access documents folder without the user's direct permission, but your usage here is not covered by the certification requirements.
--Rob
- Marked as answer by FireDance Tuesday, February 18, 2014 5:05 AM
Tuesday, February 18, 2014 1:37 AMModerator