hartk1213
hartk1213

Reputation: 33

Xamarin forms SQLite for Android

so i have already made one app that was a test app and it worked just fine using sqlite-net-pcl and i an now making an actual app and i am getting this weird error and i cant figure out why. i found the exact same question already asked on here but it didnt provide any real answer and it was over a year ago.

System.Exception: Something went wrong in the build configuration. This is the bait assembly, which is for referencing by portable libraries, and should never end up part of the app. Reference the appropriate platform assembly instead.

public SQLite.SQLiteConnection GetConnection()
    {
        var sqliteFilename = "TestDB.db3";
        string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
        var path = Path.Combine(documentsPath, sqliteFilename);
        // Create the connection
        var conn = new SQLite.SQLiteConnection(path);//HERE IS WHERE IT THROWS THE EXCEPTION
        // Return the database connection
        return conn;
    }

the app works for every other platform i have tried except android and my test app that worked just fine is using the exact same references and same class as well i just cant figure it out. i have tried referencing the sqlite-net-pcl and made a new class still doesnt work. any help would be greatly appreciated thank you very much

Upvotes: 0

Views: 211

Answers (1)

Bonelol
Bonelol

Reputation: 546

Here is the one I am using https://github.com/oysteinkrog/SQLite.Net-PCL

Check you referenced SQLite.Net.Platform.XamarinAndroid in your Android project.

Here is the code which is working fine for me (I also included SQLite.Net.Async-PCL)

public SQLiteAsyncConnection GetConnection()
        {
            // database name
            const string SQLITE_FILENAME = "MyDB.db3";

            // Get app folder
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            string path = Path.Combine(documentsPath, SQLITE_FILENAME);

            var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            var sqliteConnection = new SQLiteConnectionWithLock(plat, new SQLiteConnectionString(path, true)){BusyTimeout = TimeSpan.FromSeconds(5)};
            var conn = new SQLiteAsyncConnection(() => sqliteConnection,
                TaskScheduler.FromCurrentSynchronizationContext());

            // Return the database connection 
            return conn;
        }

Guess this should work:

public SQLiteConnection GetConnection()
        {
            // database name
            const string SQLITE_FILENAME = "MySIT.db3";

            // Get app folder
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(); //important!
            string path = Path.Combine(documentsPath, SQLITE_FILENAME);            
            var conn = new SQLiteConnection(plat, path); 

            // Return the database connection 
            return conn;
        }

Upvotes: 1

Related Questions