簡體   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