我无法在WP8的Sqlite.net的PCL版本中创建SQLiteConnection [英] I cannot create SQLiteConnection in PCL version of Sqlite.net on WP8
问题描述
我正在使用 https://github.com/的Sqlite.net的PCL版本. oysteinkrog/SQLite.Net-PCL 以下代码由于某些原因在WP8上无法正常工作:
I'm using a PCL version of Sqlite.net from https://github.com/oysteinkrog/SQLite.Net-PCL This code below doesn't work for some reasons on 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)
我做错了什么? 谢谢!!!
What I am doing wrong? Thank you!!!
推荐答案
@ Sergey-Chesalin谢谢您的回答.我检查发现它不可用时应该创建数据库.经过研究,我发现了为什么它不能正常工作.
@Sergey-Chesalin thank you for your answers. I checked and found that it should create DB when it's not available. After my research I found why it doesn't work as it should.
由于某些原因,它们会在连接字符串中添加其他符号,请参见 SQLiteConnection.cs中的第196行.也许它可以在ISQLiteApi实现的iOS/Android/WinRT版本中按预期方式工作,但不适用于Windows Phone.
For some reasons they add additional symbol to the connection string, see Line 121 in SQLiteConnection.cs and Line 196 in SQLiteConnection.cs. Maybe it works as expected in iOS/Android/WinRT versions of ISQLiteApi implementations, but it doesn't work as it should for Windows Phone.
因此,为了修复它,我更改了此行:
So in order to fix it I have changed this line:
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第12行
这篇关于我无法在WP8的Sqlite.net的PCL版本中创建SQLiteConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!