OS X 上的 Mono:System.Data.SQLite 不起作用

Mono on OS X: System.Data.SQLite does not work(OS X 上的 Mono:System.Data.SQLite 不起作用)

本文介绍了OS X 上的 Mono:System.Data.SQLite 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am planning to do a project using Mono and SQLite as the database. Development is to be done primarily on a Mac. I have successfully setup Mono and tested System.Data.SQLite(managed dll). Simple test applications work perfectly.

但是,当我尝试在我的代码中使用 DataTable 时,它会引发运行时异常.以下是代码片段:

However when I tried to use DataTable in my code it throws a runtime exception. Following is the code snippet:

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

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

The above code get built successfully but does not run on my development machine and gives the following output on the terminal.

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

The output when I run the application is as below:

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 下完美运行,但在我的开发机器上却失败了.

I was surprised to see that the same above executable worked perfectly under windows, but was failing on my development machine.

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

Searching on the internet, I found that this problem has to do something with the sqlite dynamic library and must be compiled with SQLITE_ENABLE_COLUMN_METADATA. I've tried to compile sqlite with the SQLITE_ENABLE_COLUMN_METADATA as well. I don't know where to put the compiled libsqlite3.0.dylib.

任何帮助将不胜感激.

推荐答案

你需要把dylib放在mono能找到的任何地方.

You need to put the dylib anywhere mono can find it.

您可以通过以下方式找到 mono 在哪里查找本机库:

You can find out where mono looks for native libraries by doing this:

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

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

and verbose lookup output will be printed to the terminal. On my system mono looks first in the directory where the executable is, so putting the dylib there is probably the easiest. Then mono asks the system to find the dylib (by trying to open it without a path). The system typically looks in /usr/lib and maybe a few other places (this is of course system-dependent), but in any case you can add a path for the system to look in by setting LD_LIBRARY_PATH to that path. In this case you'll do this:

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

这篇关于OS X 上的 Mono:System.Data.SQLite 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:OS X 上的 Mono:System.Data.SQLite 不起作用

基础教程推荐