【问题标题】:Xamarin forms SQLite for AndroidXamarin 为 Android 构建 SQLite
【发布时间】:2016-09-13 16:50:57
【问题描述】:

所以我已经制作了一个测试应用程序,它使用 sqlite-net-pcl 运行良好,而我现在正在制作一个实际的应用程序,但我遇到了这个奇怪的错误,我不知道为什么。我在这里找到了确切的same question,但它没有提供任何真正的答案,而且是一年多以前的事了。

System.Exception:构建配置出现问题。这是诱饵程序集,供可移植库引用,并且永远不应成为应用程序的一部分。请改为引用适当的平台程序集。

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

该应用程序适用于我尝试过的所有其他平台,除了 android,我的测试应用程序运行良好,它使用完全相同的引用和相同的类,我只是想不通。我尝试引用 sqlite-net-pcl 并创建了一个新类仍然不起作用。任何帮助将不胜感激 非常感谢你

【问题讨论】:

  • 您的共享项目和个人平台项目中是否都安装了 SQLite 包?
  • 是的,我仔细检查过,我已经卸载并重新安装了多次
  • 您是否尝试过清理并重建您的项目和解决方案?也许一些旧的/错误的 DLL 被缓存在那里
  • 您可以从输出窗口粘贴详细信息吗?
  • 是的,我已将其粘贴在问题中

标签: android xamarin xamarin.forms


【解决方案1】:

这是我正在使用的 https://github.com/oysteinkrog/SQLite.Net-PCL

检查您在 Android 项目中是否引用了 SQLite.Net.Platform.XamarinAndroid。

这是对我来说工作正常的代码(我还包括 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;
        }

猜猜这应该可行:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多