gpt4 book ai didi

c# - SQLite PRAGMA table_info(table) 不返回列名(在 C# 中使用 Data.SQLite)

转载 作者:行者123 更新时间:2023-12-03 22:55:57 26 4
gpt4 key购买 nike

我使用以下示例代码并希望以某种方式获取 sqlite3 数据库的列名:

using System;
using System.Data.SQLite;
namespace Program
{
class Program
{
static void Main(string[] args)
{
Program stuff = new Program();
stuff.DoStuff();
Console.Read();
}
private void DoStuff()
{
SQLiteConnection.CreateFile("Database.sqlite");
SQLiteConnection con = new SQLiteConnection("Data Source=Database.sqlite;Version=3;");
con.Open();
string sql = "create table 'member' ('account_id' text not null unique, 'account_name' text not null);";
SQLiteCommand command = new SQLiteCommand(sql, con);
command.ExecuteNonQuery();
sql = "insert into member ('account_id', 'account_name') values ('0', '1');";
command = new SQLiteCommand(sql, con);
sql = "PRAGMA table_info('member');";
command = new SQLiteCommand(sql, con);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetName(0));
}
con.Close();
}

}
}

我也试过
for(int i=0;i<reader.FieldCount;i++)
{
Console.WriteLine(reader.GetName(i));
}

var columns = Enumerable.Range(0, reader.FieldCount).Select(reader.GetName).ToList();

我得到的唯一结果是以下输出:
“cid 名称类型 notnull dflt_value pk”
虽然我没有得到实际的列名..
我确实需要列名,因为我正在根据来自我无权访问的另一台服务器的 API 的结果将新列写入数据库。打印数据时,我想确保显示正确的列名。

最佳答案

我真的认为 SQLite 讨厌我 - 所以而不是查询 beeing

PRAGMA table_info('member');

我现在用
SELECT * FROM member WHERE 1 = 2;

这当然只会返回表格本身而没有任何内容。
但是 - reader.GetName(i) 实际上是返回真实的列名!我只花了 5 个小时试图让 'PRAGMA table_info('table_name')' 工作来解决这个问题......

关于c# - SQLite PRAGMA table_info(table) 不返回列名(在 C# 中使用 Data.SQLite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com