Search code examples
sqlitewindows-phone-8portable-class-library

I cannot create SQLiteConnection in PCL version of Sqlite.net on 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);

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


Solution

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