使用.sqlite3文件在Windows Phone 8上创建数据库

时间:2014-01-31 14:48:30

标签: c# xaml windows-phone-8 sqlite

我一直在使用本教程作为基础在Windows手机上尝试SQLite http://code.msdn.microsoft.com/wpapps/Using-Sqlite-with-WP8-52c3c671 在其中,使用此

在App.xaml中创建数据库
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; 
}

我有一个带有我创建的数据库的database.sqlite3文件,并在Assets文件夹中添加到我的项目中。 我如何使用该文件在我的Windows手机应用程序上创建数据库?

1 个答案:

答案 0 :(得分:0)

在解决方案中添加database.sqlite3。并确保将database.sqlite3 build action属性设置为“Content”。下面的代码可以帮助您在wp8应用程序中使用已创建的数据库文件。

string dbPath = GetDbPath(dbFileName);

<强>编辑

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