繁体   English   中英

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

[英]Using .sqlite3 file to create a database on Windows Phone 8

我一直在使用本教程作为基础http://code.msdn.microsoft.com/wpapps/Using-Sqlite-with-WP8-52c3c671在Windows Phone上尝试SQLite,并在其中创建了数据库。 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 Phone应用程序上创建数据库?

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

字符串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;
     }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM