【问题标题】:Create persistent Sqlite db in Windows phone 8在 Windows phone 8 中创建持久性 Sqlite 数据库
【发布时间】:2015-05-01 11:38:06
【问题描述】:

我正在尝试使用Windows phone 8 应用程序,但我在这里陷入了一个奇怪的境地。我正在使用 sqlite 来创建 sqlite db 并将值添加到数据库中。我能够创建数据库并成功在数据库中添加值,但我在这里遇到了一个奇怪的情况。

每次我关闭模拟器并再次启动项目时,都会再次创建数据库,这不应该发生,因为我在第一次运行应用程序时创建了数据库。

有谁知道为什么,以及如何防止它每次都重新创建数据库?

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);
}

【问题讨论】:

    标签: database sqlite visual-studio-2012 windows-phone-8


    【解决方案1】:

    我很确定当您关闭模拟器并重新启动时,您基本上只是在卸载应用程序。这就是为什么您的文件不再存在的原因——因为看起来您将数据存储在独立的存储中。不知道有没有这方面的。

    您可以购买非常便宜的 Windows 8/8.1 Phone,文件将一直存在,直到您手动卸载测试应用程序。

    【讨论】:

      【解决方案2】:

      正如@Chubosaurus 所说,关闭并重新打开模拟器将删除所有应用程序。您通常可以根据需要保持它运行,并继续将您的应用程序重新部署到模拟器(尽管显然重新启动主机 PC 会杀死它)。

      您可以通过ISETool 命令保存和恢复模拟器映像中的数据。查看更多here

      【讨论】:

        【解决方案3】:

        检查 isDatabaseExisting 后尝试将 Console.WriteLine("True");Console.WriteLine("False"); 添加到预期的位置 strong> 来查看/理解代码路径到底是什么。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-05
          • 1970-01-01
          • 1970-01-01
          • 2012-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-27
          相关资源
          最近更新 更多