【问题标题】:Mono on OS X: System.Data.SQLite does not workOS X 上的 Mono:System.Data.SQLite 不起作用
【发布时间】:2011-11-11 05:47:14
【问题描述】:

我打算做一个项目,使用 Mono 和 SQLite 作为数据库。开发主要在 Mac 上完成。我已经成功设置 Mono 并测试了 System.Data.SQLite(managed dll)。简单的测试应用程序完美运行。

但是,当我尝试在我的代码中使用 DataTable 时,它会引发运行时异常。以下是代码sn-p:

public static void Main (string[] args)
{
    string connectionString = "Data Source=emp.db";

    try {
        using (SQLiteConnection conn = new SQLiteConnection(connectionString))
        {
            string query = "SELECT firstname, lastname FROM employees";

            using (SQLiteCommand comm = new SQLiteCommand(query, conn))
            {
                conn.Open();

                comm.CommandText = query;

                using (SQLiteDataReader reader = comm.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string firstname = reader.GetString(0);
                        string lastname  = reader.GetString(1);
                        Console.WriteLine("Name: " + firstname + " " + lastname);
                    }

                    DataTable dt = new DataTable();
                    dt.Load(reader);  // line 39 where problem occurs
                }
            }
        }
    } catch (Exception e) {
        Console.WriteLine(e);
    }
}

上面的代码构建成功,但没有在我的开发机器上运行,并在终端上给出以下输出。

我运行应用程序时的输出如下:

Name: John Doe
Name: Eric Smith
System.EntryPointNotFoundException: sqlite3_column_origin_name
  at (wrapper managed-to-native) System.Data.SQLite.UnsafeNativeMethods:sqlite3_column_origin_name (intptr,int)
  at System.Data.SQLite.SQLite3.ColumnOriginalName (System.Data.SQLite.SQLiteStatement stmt, Int32 index) [0x00000] in <filename unknown>:0 
  at System.Data.SQLite.SQLiteDataReader.GetSchemaTable (Boolean wantUniqueInfo, Boolean wantDefaultValue) [0x00000] in <filename unknown>:0 
  at System.Data.SQLite.SQLiteDataReader.GetSchemaTable () [0x00000] in <filename unknown>:0 
  at System.Data.Common.DataAdapter.BuildSchema (IDataReader reader, System.Data.DataTable table, SchemaType schemaType, MissingSchemaAction missingSchAction, MissingMappingAction missingMapAction, System.Data.Common.DataTableMappingCollection dtMapping) [0x0003b] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System.Data/System.Data.Common/DataAdapter.cs:284 
  at System.Data.DataTable.Load (IDataReader reader, LoadOption loadOption) [0x0001f] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System.Data/System.Data/DataTable.cs:2853 
  at System.Data.DataTable.Load (IDataReader reader) [0x00011] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System.Data/System.Data/DataTable.cs:2838 
  at SQLiteApp.MainClass.Main (System.String[] args) [0x00086] in /Users/nayaabkhan/Projects/SQLiteApp/SQLiteApp/Main.cs:37

我惊讶地发现,上面的相同可执行文件在 Windows 下完美运行,但在我的开发机器上却失败了。

在网上搜索,发现这个问题与sqlite动态库有关,必须用SQLITE_ENABLE_COLUMN_METADATA编译。我也尝试使用 SQLITE_ENABLE_COLUMN_METADATA 编译 sqlite。我不知道把编译好的libsqlite3.0.dylib放在哪里。

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 SO!对于第一次提问的人来说,这是一个写得很好的问题。我向你倾斜我的帽子。
  • 嘿,感谢 lnuyasha 的赞赏。
  • 您在哪里下载了托管库?我正在尝试在mac上找一个地方下载它,但找不到。

标签: c# sqlite mono


【解决方案1】:

您需要将 dylib 放在 mono 可以找到的任何地方。

您可以通过执行以下操作找出 mono 在哪里查找本机库:

export MONO_LOG_LEVEL=debug
export MONO_LOG_MASK=dll
mono yourprogram.exe

详细查找输出将打印到终端。在我的系统上,mono 首先在可执行文件所在的目录中查找,因此将 dylib 放在那里可能是最简单的。然后 mono 要求系统找到 dylib(通过尝试在没有路径的情况下打开它)。系统通常会在 /usr/lib 和其他一些地方查找(这当然取决于系统),但无论如何,您都可以通过将 LD_LIBRARY_PATH 设置为该路径来添加系统查找的路径。在这种情况下,您将这样做:

export LD_LIBRARY_PATH=/path/to/dylib:$LD_LIBRARY_PATH
mono yourprogram.exe

【讨论】:

  • 解决了。我将 dylib 放在 /usr/lib 中,mono 找到了它。谢谢罗尔夫。
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多