我一直在尝试创建一个 Windows Phone,我想使用 SQLite 来存储我的数据并学习如何在 Windows Phone 应用程序上使用它。为此,我使用“SQLite.Net-PCL”,但我不断收到文件未找到异常。这是我写的代码:
String ConnectionString = Path.Combine(ApplicationData.Current.LocalFolder.Path, Connection);
if (File.Exists(ConnectionString))
{
SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
Con = new SQLiteConnection(e,ConnectionString);
}
else {
SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8();
File.Create(ConnectionString);
Con = new SQLiteConnection(e, ConnectionString);
}
我想也许我会收到此错误,因为我手动创建了一个空文件,但如果这是问题所在,如果手机中不存在数据库,我该如何创建数据库?
您不需要自己创建该文件,因为 SQLiteConnection 构造函数会为您管理该文件。
public SQLiteConnection(ISQLitePlatform sqlitePlatform, string databasePath, bool storeDateTimeAsTicks = false, IBlobSerializer serializer = null)
: this(
sqlitePlatform, databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, serializer)
{
}
因此,您应该打开连接,创建表,就这样。
class ExampleDataContext
{
public const string DATABASE_NAME = "data.sqlite";
private SQLiteConnection connection;
public TableQuery<Foo> FooTable { get; private set; }
public TableQuery<Bar> BarTable { get; private set; }
public ExampleDataContext()
{
connection = new SQLiteConnection(new SQLitePlatformWinRT(), Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DATABASE_NAME));
Initialize();
FooTable = connection.Table<Foo>();
BarTable = connection.Table<Bar>();
}
private void Initialize()
{
connection.CreateTable<Foo>();
connection.CreateTable<Bar>();
}
}
不用担心这个Initialize
,只有当表还不存在时才会创建它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)