使用 Delphi (Dbexpress) 获取列名称

发布于 2024-09-11 09:47:27 字数 292 浏览 4 评论 0原文

我正在使用这个 sql 命令来获取列名:

select COLUMN_NAME from 
INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'MyTableName'

但我不知道如何使用执行的 SQL 命令结果!

例如,这种方法无法将列名提取为字符串值,并且我收到此错误=不支持操作:

  for i := 1 to Qry1.RecordCount do
  begin

  end;

I'm using this sql command to get column names :

select COLUMN_NAME from 
INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'MyTableName'

but i don't know how can i using the executed SQL command results !

for example , this way doesn't work to extract the column names as a string value and i got this error = Operation Not Supported :

  for i := 1 to Qry1.RecordCount do
  begin

  end;

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

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

发布评论

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

评论(4

梦巷 2024-09-18 09:47:27

另一种方法是查询表本身以获取空数据集,然后循环访问该数据集中的字段。

像这样的查询将返回其中没有记录的表结构:

Qry1.SQL.Text := 'SELECT * FROM MyTableName WHERE 1<>1';
Qry1.Open;

并且像这样的循环将迭代每个字段

for I := 0 to Qry1.FieldCount-1 do
begin
  X := Qry1.Fields[I].FieldName;
  // and do whatever you want with X
end;

Another way you can do this is to query the table itself to get an empty dataset, and then loop through the fields in that dataset.

A query like this will return the table structure with no records in it:

Qry1.SQL.Text := 'SELECT * FROM MyTableName WHERE 1<>1';
Qry1.Open;

And a loop like this will iterate through each field

for I := 0 to Qry1.FieldCount-1 do
begin
  X := Qry1.Fields[I].FieldName;
  // and do whatever you want with X
end;
旧街凉风 2024-09-18 09:47:27

像这样的东西适用于 TADOQuery (不确定它对于 dbExpress 是否不同):

Qry1.Open;
while not Qry1.Eof do begin
    // do whatever with Qry1.Fields[0].AsString here
    Qry1.Next;
end;
Qry1.Close;

Something like this would work for a TADOQuery (not sure if it's different for dbExpress):

Qry1.Open;
while not Qry1.Eof do begin
    // do whatever with Qry1.Fields[0].AsString here
    Qry1.Next;
end;
Qry1.Close;
酷到爆炸 2024-09-18 09:47:27

据我了解,您无法检索结果。

Qry1.First;

while not Qry1.Eof do
begin
    X := Qry1.FieldByName('column_name').AsString;

    Qry1.Next;
end;

这是一段一直对我有用的代码

或者您可以阅读此链接,它解释了为什么在调用 .RecordCount (http://edn.embarcadero.com/article/28494

总而言之,它表明您的查询区分大小写,您应该检查表名称(MyTableName)

From what I understand you are unable to retreive the retults.

Qry1.First;

while not Qry1.Eof do
begin
    X := Qry1.FieldByName('column_name').AsString;

    Qry1.Next;
end;

This is a piece of code which has always worked for me

Or you can read this link which explains why the exception is thrown when calling .RecordCount (http://edn.embarcadero.com/article/28494)

To sum it up it suggests that your query is case-sensitive and you should probably check the table name (MyTableName)

绳情 2024-09-18 09:47:27

同意 Rob McDonell 的观点,为了列出字段的列名称,我将使用它
就像在我的代码中我写了这样的东西

Procedure blablabla;
var i:integer;
begin
..... {some code here}

SQLQuery1.Open;
  for i := 0 to SQLQuery1.FieldCount-1 do
    begin;
    Memo1.Lines.Append(SQLQuery1.Fields[i].DisplayName);
    end;
  SQLQuery1.Close;


.... {some code here}

end;

agree with Rob McDonell, in order to list the column name of a field, I'll use that
as in my code I wrote something like this

Procedure blablabla;
var i:integer;
begin
..... {some code here}

SQLQuery1.Open;
  for i := 0 to SQLQuery1.FieldCount-1 do
    begin;
    Memo1.Lines.Append(SQLQuery1.Fields[i].DisplayName);
    end;
  SQLQuery1.Close;


.... {some code here}

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