0
votes

I've been trying out SQLite on windows phone using this tutorial as base http://code.msdn.microsoft.com/wpapps/Using-Sqlite-with-WP8-52c3c671 and in it, the Database is created in the App.xaml with this

string dbPath = Path.Combine(
    Windows.Storage.ApplicationData.Current.LocalFolder.Path,
    "db.sqlite"); 
if (!FileExists("db.sqlite").Result) 
{ 
    using (var db = new SQLiteConnection(dbPath)) 
    { 
        db.CreateTable<Person>(); 
    } 
}

private async Task<bool> FileExists(string fileName) 
{ 
    var result = false; 
    try 
    { 
        var store = await Windows
            .Storage.ApplicationData.Current.LocalFolder
            .GetFileAsync(fileName); 
        result =true; 
    } 
    catch { }
    return result; 
}

I have a database.sqlite3 file with a database I created, and added to my project on the Assets folder. How i can use that file to create the database on my windows phone app ?

1

1 Answers

0
votes

Add database.sqlite3 in you solution. and Make sure database.sqlite3 build action property set to 'Content'. Below is the code that help you a lot to use a created database file in wp8 apps.

string dbPath = GetDbPath(dbFileName);

Edits

private async string GetDbPath( string fileName)
  {
   if (await DoesFileExistAsync(fileName))
      {
       // file exists;
       string  DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
        fileName);
     }
   else
      {
      // file does not exist
       StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(fileName);
      await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
string  DBPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName);
      }
}

private async Task<bool> DoesFileExistAsync(string fileName)
   {
     try
      {
       await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
        return true;
       }
     catch
     {
      return false;
     }
  }