JAX*_*JAX 5 c# sqlite windows-phone-8
我一直在尝试创建一个Windows手机,我想使用SQLite存储我的数据,并学习如何在Windows手机应用程序上使用它.为此我使用"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);
}
Run Code Online (Sandbox Code Playgroud)
我想也许我得到这个错误,因为我手动创建一个空文件,但如果这是问题,如果手机中没有数据库,我怎么能创建一个数据库?
您不需要自己创建该文件,因为 SQLiteConnection 构造函数会为您管理该文件。
public SQLiteConnection(ISQLitePlatform sqlitePlatform, string databasePath, bool storeDateTimeAsTicks = false, IBlobSerializer serializer = null)
: this(
sqlitePlatform, databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, serializer)
{
}
Run Code Online (Sandbox Code Playgroud)
因此,您应该打开连接,创建表,就这样。
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>();
}
}
Run Code Online (Sandbox Code Playgroud)
不用担心Initialize,只有当表还不存在时才会创建它们。