0

我的Windows應用程序(位於Windows應用商店)的第一個版本中有一個SQLite數據庫。現在我想發佈應用程序的第二個版本,該應用程序中還添加了一個SQLite數據庫。 我有我的數據保存在第一個版本,不想失去它們。 我發現AndroidonCreateonUpgrade處理sqlite數據庫版本的方法。 Android: upgrading DB version and adding new tableWindows運行時應用程序:升級SQLite數據庫並添加新表

類似的問題是here。但是這是針對iOS的。

是否有針對Windows運行時應用程序(Windows 8.1和Windows Phone 8.1)的類似解決方案?請提出一些替代方案。

在此先感謝。

+0

[升級應用程序更新數據庫]的可能重複(http://stackoverflow.com/questions/15703352/upgrading-database-on-app-update) –

回答

1

一個更好的(性能)的方式有一個版本的DB是使用「PRAGMA user_version」

var sqLiteAsyncConnection = new SQLiteAsyncConnection(path); 

// read the user version 
var version = sqLiteAsyncConnection.ExecuteScalar<string>("PRAGMA user_version"); 

perfMon.TraceSinceLast("StandardQueryBase : db version read"); 

if (version == "0") 
{ 
    // update the schema here 

    // store the new version number in the DB 
    sqLiteAsyncConnection.ExecuteScalar<string>("PRAGMA user_version=1;"); 
} 
+1

謝謝@ Jmix90。這對性能有好處,並以您的答案結束。再次感謝 :) –

2

處理這類問題的一個好方法是在數據庫中添加一個版本控制系統。 在使用與數據庫的連接之前,只需檢查數據庫中的應用程序版本,並且如果新版本高於前一版本,則運行所有必要的命令來更新數據庫。

例:

public async Task<SQLite.SQLiteConnection> GetSqliteConnectionForUserAsync(string login) 
{ 
    using (await _mutex.LockAsync()) 
    { 
     if (login == null) 
     { 
      login = "__anonymous__"; 
     } 

     SQLite.SQLiteConnection conn; 
     if (!_userConnections.TryGetValue(login, out conn)) 
     { 
       conn = new SQLite.SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, 
       string.Format("{0}.db", Uri.EscapeDataString(login)))); 
       await SqlSchemaHandler.EnsureSchemaReadyAsync(conn, s => 
       { 
        _logger.Info("Schema handler message : {0}", s); 
       }); 
      _userConnections[login] = conn; 
     } 
    return conn; 
} 

}

和(SqlSchemaHandler):

public static Task EnsureSchemaReadyAsync(SQLiteConnection connection, Action<string> actionReporter) 
{ 
    return Task.Run(() => 
    { 
     connection.CreateTable<SchemaInfo>(); 
     var schemaInfo = connection.Table<SchemaInfo>().FirstOrDefault(); 
     if (schemaInfo == null) 
     { 
      ApplyV0ToV1(connection); 
      schemaInfo = new SchemaInfo { Id = 1, Version = 1 }; 
      connection.Insert(schemaInfo); 
     } 
    }); 
} 

private static void ApplyV0ToV1(SQLiteConnection connection) 
{ 
    connection.CreateTable<Test>(); 
} 

感謝,

+0

感謝您的建議!感謝你的努力。是的,這是一個更好的主意。 @Thomas LEBRUN –

相關問題