I cannot create SQLiteConnection in PCL version of Sqlite.net on WP8

人走茶凉 提交于 2019-12-22 17:36:14

问题


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

And I am receiving this error:

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!!!


回答1:


@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.

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

To this:

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

in the SQLite.Net-PCL/blob/master/src/SQLite.Net.Platform.WindowsPhone8.CSharpSqlite/SQLiteApiWP8.cs line 12




回答2:


Have you marked your database file file as content in project? And you can explore deployed project with ISETool or something with GUI like this to check for file existence by this path.



来源:https://stackoverflow.com/questions/20873946/i-cannot-create-sqliteconnection-in-pcl-version-of-sqlite-net-on-wp8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!