我无法在WP8上的PCL版本的Sqlite.net中创建SQLiteConnection

时间:2014-01-01 22:27:51

标签: sqlite windows-phone-8 portable-class-library

我正在使用https://github.com/oysteinkrog/SQLite.Net-PCL的PCL版Sqlite.net 以下代码在WP8上由于某些原因不起作用:

var sqliteFilename = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyDB.db3");
var db = new SQLiteConnection(new SQLitePlatformWP8CSharp(), sqliteFilename);

我收到此错误:

An exception of type 'SQLite.Net.SQLiteException' occurred in SQLite.Net.DLL but was not handled in user code

Additional information: Could not open database file: C:\Data\Users\DefApps\AppData\{149B7F85-2C71-4BEF-984F-903BA7DB80DA}\Local\MyDB.db3 (CannotOpen)

我做错了什么? 谢谢!!!

2 个答案:

答案 0 :(得分:1)

@ Sergey-Chesalin感谢您的回答。我查了一下,发现它应该在它不可用时创建数据库。在我的研究之后,我发现为什么它不能正常工作。

由于某些原因,他们会在连接字符串中添加其他符号,请参阅Line 121 in SQLiteConnection.csLine 196 in SQLiteConnection.cs。也许它在iOS / Android / WinRT版本的ISQLiteApi实现中按预期工作,但它不能像Windows Phone那样工作。

所以为了解决这个问题,我改变了这一行:

string dbFileName = Encoding.UTF8.GetString(filename, 0, filename.Length);

对此:

string dbFileName = Encoding.UTF8.GetString(filename, 0, filename.Length - 1);
SQLite.Net-PCL/blob/master/src/SQLite.Net.Platform.WindowsPhone8.CSharpSqlite/SQLiteApiWP8.cs line 12

中的

答案 1 :(得分:0)

您是否已将数据库文件文件标记为项目中的内容? 您可以使用ISETool或类似this的GUI来探索已部署的项目,以通过此路径检查文件是否存在。