获取表架构似乎不适用于 System.Data.SQLite

发布于 2024-09-10 09:41:00 字数 2258 浏览 4 评论 0原文

我使用 SQLite.NET 访问数据库,在一种特殊情况下,我需要获取表架构,因为它是特定于应用程序的。

我一直在浏览 SQLite.NET 论坛和谷歌,它看起来非常简单。如果我有一个名为 MYTABLE 的表,我应该能够执行此操作:

DataTable dt = Connection.GetSchema( SQLiteMetaDataCollectionNames.Columns, new string[] { null, null, "MYTABLE", null });

问题是,虽然我确实得到了一个 DataTable,但其中包含所有错误的信息。具体来说,这是我为 MYTABLE 假定的列返回的内容:

  • [0] {TABLE_CATALOG} object {System.Data.DataColumn}
  • [1] {TABLE_SCHEMA} object {System.Data.DataColumn}
  • [2] {TABLE_NAME} object {System.Data.DataColumn}
  • [3] {COLUMN_NAME} 对象 {System.Data.DataColumn}
  • [4] {COLUMN_GUID} 对象 {System.Data.DataColumn}
  • [5] {COLUMN_PROPID} 对象 {System.Data.DataColumn}
  • [ 6] {ORDINAL_POSITION} 对象 {System.Data.DataColumn}
  • [7] {COLUMN_HASDEFAULT} 对象 {System.Data.DataColumn}
  • [8] {COLUMN_DEFAULT} 对象 {System.Data.DataColumn}
  • [9] {COLUMN_FLAGS} 对象 {System .Data.DataColumn}
  • [10] {IS_NULLABLE} 对象 {System.Data.DataColumn}
  • [11] {DATA_TYPE} 对象 {System.Data.DataColumn}
  • [12] {TYPE_GUID} 对象 {System.Data.DataColumn}
  • [13] {CHARACTER_MAXIMUM_LENGTH} 对象 {System.Data.DataColumn}
  • [14] {CHARACTER_OCTET_LENGTH} 对象 {System.Data.DataColumn}
  • [15] {NUMERIC_PRECISION} 对象 {System.Data.DataColumn}
  • [16] {NUMERIC_SCALE} 对象 {System.Data .DataColumn}
  • [17] {DATETIME_PRECISION} 对象 {System.Data.DataColumn}
  • [18] {CHARACTER_SET_CATALOG} 对象 {System.Data.DataColumn}
  • [19] {CHARACTER_SET_SCHEMA} 对象 {System.Data.DataColumn}
  • [20] {CHARACTER_SET_NAME } 对象 {System.Data.DataColumn}
  • [21] {COLLATION_CATALOG} 对象 {System.Data.DataColumn}
  • [22] {COLLATION_SCHEMA} 对象 {System.Data.DataColumn}
  • [23] {COLLATION_NAME} 对象 {System.Data.DataColumn }
  • [24] {DOMAIN_CATALOG} 对象 {System.Data.DataColumn}
  • [25] {DOMAIN_NAME} 对象 {System.Data.DataColumn}
  • [26] {DESCRIPTION} 对象 {System.Data.DataColumn}
  • [27] {PRIMARY_KEY} 对象{System.Data.DataColumn}
  • [28] {EDM_TYPE} 对象 {System.Data.DataColumn}
  • [29] {AUTOINCRMENT} 对象 {System.Data.DataColumn}
  • [30] {UNIQUE} 对象 {System.Data.DataColumn}

可以有人告诉我我在这里做错了什么吗?

I'm using SQLite.NET to access my databases, and in one particular case, I need to get a table schema because it's application-specific.

I've been looking through the SQLite.NET forum and google, and it seems very straightforward. If I have a table called MYTABLE, I should be able to do this:

DataTable dt = Connection.GetSchema( SQLiteMetaDataCollectionNames.Columns, new string[] { null, null, "MYTABLE", null });

The problem is that, although I do get a DataTable back, it's got all of the wrong information in it. Specifically, this is what I get back for the supposed columns of MYTABLE:

  • [0] {TABLE_CATALOG} object {System.Data.DataColumn}
  • [1] {TABLE_SCHEMA} object {System.Data.DataColumn}
  • [2] {TABLE_NAME} object {System.Data.DataColumn}
  • [3] {COLUMN_NAME} object {System.Data.DataColumn}
  • [4] {COLUMN_GUID} object {System.Data.DataColumn}
  • [5] {COLUMN_PROPID} object {System.Data.DataColumn}
  • [6] {ORDINAL_POSITION} object {System.Data.DataColumn}
  • [7] {COLUMN_HASDEFAULT} object {System.Data.DataColumn}
  • [8] {COLUMN_DEFAULT} object {System.Data.DataColumn}
  • [9] {COLUMN_FLAGS} object {System.Data.DataColumn}
  • [10] {IS_NULLABLE} object {System.Data.DataColumn}
  • [11] {DATA_TYPE} object {System.Data.DataColumn}
  • [12] {TYPE_GUID} object {System.Data.DataColumn}
  • [13] {CHARACTER_MAXIMUM_LENGTH} object {System.Data.DataColumn}
  • [14] {CHARACTER_OCTET_LENGTH} object {System.Data.DataColumn}
  • [15] {NUMERIC_PRECISION} object {System.Data.DataColumn}
  • [16] {NUMERIC_SCALE} object {System.Data.DataColumn}
  • [17] {DATETIME_PRECISION} object {System.Data.DataColumn}
  • [18] {CHARACTER_SET_CATALOG} object {System.Data.DataColumn}
  • [19] {CHARACTER_SET_SCHEMA} object {System.Data.DataColumn}
  • [20] {CHARACTER_SET_NAME} object {System.Data.DataColumn}
  • [21] {COLLATION_CATALOG} object {System.Data.DataColumn}
  • [22] {COLLATION_SCHEMA} object {System.Data.DataColumn}
  • [23] {COLLATION_NAME} object {System.Data.DataColumn}
  • [24] {DOMAIN_CATALOG} object {System.Data.DataColumn}
  • [25] {DOMAIN_NAME} object {System.Data.DataColumn}
  • [26] {DESCRIPTION} object {System.Data.DataColumn}
  • [27] {PRIMARY_KEY} object {System.Data.DataColumn}
  • [28] {EDM_TYPE} object {System.Data.DataColumn}
  • [29] {AUTOINCREMENT} object {System.Data.DataColumn}
  • [30] {UNIQUE} object {System.Data.DataColumn}

Can anyone tell me what I've done wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘年祭陌 2024-09-17 09:41:00

SQLiteConnection.GetSchema 很可能已损坏;大多数程序不需要此功能。

您可以通过执行 PRAGMA table_info(MYTABLE) 命令来获取所需内容;您应该获得一个每列一行的数据读取器。

It's quite possible that SQLiteConnection.GetSchema is broken; most programs do not need this functionality.

You can get what you need by executing a PRAGMA table_info(MYTABLE) command; you should get a data reader with one row for each column.

落花浅忆 2024-09-17 09:41:00

以下是如何使用 PRAGMA table_info(tableName) 按照 Stephen Cleary 的建议检查指定表中是否存在指定列。

/// <summary>
/// Checks if the given table contains a column with the given name.
/// </summary>
/// <param name="tableName">The table in this database to check.</param>
/// <param name="columnName">The column in the given table to look for.</param>
/// <param name="connection">The SQLiteConnection for this database.</param>
/// <returns>True if the given table contains a column with the given name.</returns>
public static bool ColumnExists(string tableName, string columnName, SQLiteConnection connection)
{
    var cmd = new SQLiteCommand("PRAGMA table_info(" + tableName + ")", connection);
    var dr = cmd.ExecuteReader();
    while (dr.Read())//loop through the various columns and their info
    {
        var value = dr.GetValue(1);//column 1 from the result contains the column names
        if (columnName.Equals(value))
        {
            dr.Close();
            return true;
        }
    }

    dr.Close();
    return false;
}

Here is an example of how to use the PRAGMA table_info(tableName) as suggested by Stephen Cleary to check if a specified column exists in a specified table.

/// <summary>
/// Checks if the given table contains a column with the given name.
/// </summary>
/// <param name="tableName">The table in this database to check.</param>
/// <param name="columnName">The column in the given table to look for.</param>
/// <param name="connection">The SQLiteConnection for this database.</param>
/// <returns>True if the given table contains a column with the given name.</returns>
public static bool ColumnExists(string tableName, string columnName, SQLiteConnection connection)
{
    var cmd = new SQLiteCommand("PRAGMA table_info(" + tableName + ")", connection);
    var dr = cmd.ExecuteReader();
    while (dr.Read())//loop through the various columns and their info
    {
        var value = dr.GetValue(1);//column 1 from the result contains the column names
        if (columnName.Equals(value))
        {
            dr.Close();
            return true;
        }
    }

    dr.Close();
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文