如何获取 SQL 查询的结果?

发布于 2024-12-22 00:05:37 字数 454 浏览 0 评论 0原文

使用 Delphi 2010,我使用 TSQLQuery 和 TSQLConnection 连接到远程 MySQL 服务器。我使用了如下 SQL 查询:

SQLQuery1.SQL.Text := 'SELECT * FROM registered WHERE email="'+email+'" and login_pass="'+password+'"';

SQLQuery1.Open; // Open sql connection

我应该如何列出或显示此查询选择的数据?

当我输入时,

SQLQuery1['who']; // The resault is : James Kan

我认为它正在显示列表中的最后一项。但我想显示每个项目,就像使用 PHP 中的 foreach 循环一样。例如,我如何为每个项目创建一个 TLabel?

Using Delphi 2010, I have used TSQLQuery and TSQLConnection to connect to a remote MySQL server. I have used an SQL query as follows:

SQLQuery1.SQL.Text := 'SELECT * FROM registered WHERE email="'+email+'" and login_pass="'+password+'"';

SQLQuery1.Open; // Open sql connection

What should I do to list or display the data selected by this query?

When I type

SQLQuery1['who']; // The resault is : James Kan

I think it is displaying the very last item in the list. But I want to display each item, as I could with the foreach loop in PHP. How could I create, for example, a TLabel for each item?

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

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

发布评论

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

评论(2

徒留西风 2024-12-29 00:05:37

您只需迭代结果集即可

SQLQuery1.Open;
SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
   // do something with the current record
   ...
   // move to the next record
   SQLQuery1.Next;
end;

You just iterate over the resultset like

SQLQuery1.Open;
SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
   // do something with the current record
   ...
   // move to the next record
   SQLQuery1.Next;
end;
心房的律动 2024-12-29 00:05:37

您可以使用 TDBGrid 来非常整齐地显示结果。您需要一个通过数据源的 DataSet 属性链接到您的查询的数据源。然后,DBGrid 通过 DBGrid 的 DataSource 属性链接到数据源。

You can use a TDBGrid to display the results quite neatly. You need a datasource which links to your query via the DataSet property of the datasource. Then the DBGrid links to the dataSource via the DataSource Property of the DBGrid.

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