SQLite 元数据查询失败 Microsoft.Data.Sqlite,适用于 System.Data.SQLite

Sqlite metadata query fails with Microsoft.Data.Sqlite, works with System.Data.SQLite

我有一个直接来自 Microsoft 文档(请参阅 https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/metadata)的简单查询失败了。根据文档,针对 sqlite_master 的查询应该按预期工作,但它们会抛出异常,即使在我创建数据库并创建 table.

之后也是如此
SELECT t.name AS tbl_name, c.name, c.type, c.[notnull], c.dflt_value, c.pk FROM sqlite_master AS t, pragma_table_info(t.name) AS c WHERE t.type = 'table';

这是突出显示问题的独立代码:

using System;
using System.Data;
using Microsoft.Data.Sqlite;

namespace Test.Library
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string connStr = "Data Source=test.db";

                string tableQuery = "CREATE TABLE IF NOT EXISTS 'company' (id Integer PRIMARY KEY AUTOINCREMENT NOT NULL , name Text COLLATE NOCASE , postal Integer);";
                string metadataQuery = "SELECT t.name AS tbl_name, c.name, c.type, c.[notnull], c.dflt_value, c.pk FROM sqlite_master AS t, pragma_table_info(t.name) AS c WHERE t.type = 'table';";

                using (SqliteConnection conn = new SqliteConnection(connStr))
                {
                    conn.Open();
                    DataTable result = Query(tableQuery, conn);
                    Console.WriteLine(result.Rows.Count);

                    result = Query(metadataQuery, conn);
                    Console.WriteLine(result.Rows.Count);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.ReadLine();
        }

        private static DataTable Query(string query, SqliteConnection conn)
        {
            using (SqliteCommand cmd = new SqliteCommand(query, conn))
            {
                using (SqliteDataReader rdr = cmd.ExecuteReader())
                {
                    DataTable result = new DataTable();
                    result.Load(rdr);
                    return result;
                }
            }
        }
    }
}

控制台输出包括异常(注意第一行是执行第一个查询返回的行数的Console.WriteLine,即0)...

0
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table column: pragma_table_info.name'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteDataReader.GetSchemaTable()
   at System.Data.ProviderBase.SchemaMapping..ctor(DataAdapter adapter, DataSet dataset, DataTable datatable, DataReaderContainer dataReader, Boolean keyInfo, SchemaType schemaType, String sourceTableName, Boolean gettingData, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.FillMappingInternal(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.FillMapping(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 schemaCount, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
   at System.Data.DataTable.Load(IDataReader reader)
   at Test.Library.Program.Query(String query, SqliteConnection conn) in C:\Code\Misc\Program.cs:line 43
   at Test.Library.Program.Main(String[] args) in C:\Code\Misc\Program.cs:line 24

请注意,metadataQuery 直接来自 Microsoft 的站点(上面的 link)。

当我使用 DB Browser for Sqlite(或类似的)执行相同的查询时,它工作正常,并且,当使用相同的代码和 System.Data.SQLite 时,它工作正常(显然 class 名称略有不同的大小写等)。

谢谢!

Microsoft.Data.Sqlite 如何处理从 table 值函数(如 pragma_table_info.

查询数据 directly 的问题似乎是个问题

另一方面,为了准确获取所有 table 的所有元数据信息,以下查询有效(使用 nuget version 3.1.4 测试)

string metadataQuery = "DROP TABLE IF EXISTS info;" +
                       "CREATE TEMPORARY TABLE info AS SELECT t.name AS tbl_name, c.name, c.type, c.[notnull], c.dflt_value, c.pk FROM sqlite_master AS t, pragma_table_info(t.name) AS c WHERE t.type = 'table';" +
                       "SELECT * FROM  info";

有关类似问题,请参阅

此外 Microsoft.Data.Sqlite limitations 获取模式信息。