[英]Exception while creating SQLite database
I develop a Universal app that use a local SQLite database. 我开发了使用本地SQLite数据库的通用应用程序。 As I need to know the database version, I store an additional information that contains this information. 由于需要了解数据库版本,因此我存储了包含此信息的其他信息。
At the application startup, I check if the database exists, and if the database version has changed. 在应用程序启动时,我检查数据库是否存在以及数据库版本是否已更改。
public sealed partial class App : Application
{
// DB Name
public static string DBFileName = "myDb.sqlite";
public static string DBpath;
// DB Version
public static string DBVersionFilename = "myDb.version";
public static string DBVersionPath;
public static string DBVersion = "1.1.0";
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
ManageDB();
}
private async Task ManageDB()
{
if (!CheckDbExist().Result)
{
CreateDb();
}
else
{
if (CheckDbVersionUpdated().Result)
{
await DeleteDb();
CreateDb();
}
}
}
private async Task<bool> CheckDbExist()
{
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBFileName);
DBpath = storageFile.Path;
return true;
}
catch { }
return false;
}
private void CreateDb()
{
try
{
StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
DBpath = storageFile.Path;
var dbconnection = new SQLiteAsyncConnection(DBpath, false);
dbconnection.CreateTableAsync<Article>();
//...
StorageFile file = ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting).GetResults();
FileIO.WriteTextAsync(file, DBVersion);
}
catch (Exception e)
{ }
}
private async Task<bool> DeleteDb()
{
try
{
StorageFile storageFile = ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists).GetResults();
await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
return true;
}
catch { }
return false;
}
private async Task<bool> CheckDbVersionUpdated()
{
try
{
string userDBVersion;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(DBVersionFilename);
userDBVersion = await FileIO.ReadTextAsync(file);
if (userDBVersion != DBVersion)
return true;
else
return false;
}
catch { }
return false;
}
}
There is a problem on CreateDB()
: 在CreateDB()
上存在问题:
var dbconnection = new SQLiteAsyncConnection(DBpath, false);
但是,如果我不放置断点,则在调用var dbconnection = new SQLiteAsyncConnection(DBpath, false);
之后会遇到异常var dbconnection = new SQLiteAsyncConnection(DBpath, false);
: : System.InvalidOperationException: A method was called at an unexpected time. System.InvalidOperationException:在意外的时间调用了一个方法。 A method was called at an unexpected time. 在意外的时间调用了一个方法。 at Windows.Foundation.IAsyncOperation`1.GetResults() at TestRating.App.CreateDb()} 在Windows.Foundation.IAsyncOperation`1.GetResults()在TestRating.App.CreateDb()}
Would you have an idea about the encountered problem? 您对遇到的问题有一个想法吗? Is there another way to do this? 还有另一种方法吗?
When I remove " .Results()" it seems that the " CreateDb() " function works correctly: 当我删除“ .Results()”时 ,似乎“ CreateDb() ”函数可以正常工作:
private async Task<bool> CreateDb() { try { StorageFile storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBFileName, CreationCollisionOption.OpenIfExists); DBpath = storageFile.Path; var dbconnection = new SQLiteAsyncConnection(DBpath, false); dbconnection.CreateTableAsync<Article>(); //... StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(DBVersionFilename, CreationCollisionOption.ReplaceExisting); FileIO.WriteTextAsync(file, DBVersion); return true; } catch (Exception e) { } return false; }
Do you have an explanation? 你有解释吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.