我正在尝试使用Windows phone 8应用程序,我陷入了一个奇怪的境地。我使用sqlite来创建sqlite数据库并向数据库中添加值。我能够成功地创建数据库并在数据库中添加值,但是我遇到了一个奇怪的情况。
每次我关闭仿真器并再次启动项目时,都会再次创建数据库,这不应该发生,因为我在第一次运行应用程序时就创建了数据库。
有人知道为什么,以及如何防止它每次都重新创建数据库吗?

public string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "aa.sqlite"));
private SQLiteConnection dtCon;

public MainPage()
{
InitializeComponent();
CreateDatabase();
dtCon = new SQLiteConnection(DB_PATH);
var tp = dtCon.Query<Contacts>("select * from contacts").ToList();
}

private async void CreateDatabase()
{
bool isDatabaseExisting = false;
//Checking if database already exists
try
{
Windows.Storage.StorageFile storagefile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("aa.sqlite");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
//if not exists then creating database
if (!isDatabaseExisting)
{
String str = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "sqlite.db");
AddDataToDB(DB_PATH);
}
}
private void AddDataToDB(string str)
{
// Create the database connection.
dtCon = new SQLiteConnection(str);
// Create the table Task, if it doesn't exist.
dtCon.CreateTable<Contacts>();
Contacts oContacts = new Contacts();
oContacts.Name = "dfgdf";
oContacts.Detail = "asdfsf";
dtCon.Insert(oContacts);
}

最佳答案

正如@Chubosaurus所说,关闭并重新打开模拟器将删除所有应用程序。你通常可以让它运行多久,你想,并继续重新部署你的应用到模拟器(虽然显然重新启动主机将杀死它)。
您可以通过ISETool命令保存和恢复模拟器图像中的数据。查看更多here

关于database - 在Windows Phone 8中创建持久的Sqlite数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29986159/

10-13 06:49
查看更多