显示来自Delphi(RAD Studio)的数据库结构 [英] Display database structure from Delphi (rad studio)

查看:289
本文介绍了显示来自Delphi(RAD Studio)的数据库结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序.当我执行它时,什么也没有发生.为什么会这样?

Here is my procedure. When I execute it nothing happens; why is this?

 FData.FDQuery1.SQL.Clear;
 FData.FDQuery1.SQL.Add('select StrDBName FROM INFORMATION_SCHEMA.TABLES');

 FData.FDQuery1.ExecSQL;

 while FData.FDQuery1.Eof do
    ShowMessage(FData.FDQuery1.Fields[0].ToString);
     end;

推荐答案

正如在注释中向您解释的那样,您的while循环应类似于以下内容:

As already explained to you in comments, your while loop should look something like this:

 while **not** FData.FDQuery1.Eof do **begin**
    ShowMessage(FData.FDQuery1.Fields[0].ToString);
    **FData.FDQuery1.Next;**
 end;

(当然要减去星号).但是,这不能解决您的SQL错误的问题.

(minus the asterisks, of course). However, that would not overcome the problem that your SQL is incorrect.

因此,请尝试以下操作:

So, try this instead:

  1. 在一个新的Delphi项目中,放置一个TFDConnection,TFDQuery,TDataSource, TDataSource和窗体上的TListBox.保存表单和项目.

  1. In a new Delphi project, place a TFDConnection, TFDQuery, TDataSource, TDataSource and a TListBox on a form. Save the form and project.

双击FDConnection1以弹出其连接编辑器并进行配置 它,以便您可以成功地将其连接到数据库.

Double-click FDConnection1 to pop up its connection editor and configure it so you can successfully connect it to your database.

将DBGrid1连接到DataSource1,将Datasource1连接到FDQuery1.

Connect DBGrid1 to DataSource1 and Datasource1 to FDQuery1.

将下面的代码添加到表单的OnCreate事件中.

Add the code below to the form's OnCreate event.

编译并运行.

您应该立即查看问题的原因.作为错误信息 告诉您,INFORMATION_SCHEMA.TABLES表中没有strDBName字段.

You should immediately see the cause of your problem. As the error message told you, there is no strDBName field in the INFORMATION_SCHEMA.TABLES table.

因此,您需要回溯到MySQL在线帮助,例如从这里

So you need to backtrack to the MySQL online help, starting e.g. here

https://dev.mysql.com/doc/refman/5.7/en/tables-table.html

准确找出您要寻找的是什么, 如果您还不了解,以及如何从您的项目中获取它.

and work out exactly what it is that you are looking for, if you don't already know, and how to get it from within your project.

顺便说一句,如果不确定自己在做什么,则应始终首先在MySql Workbench实用程序中尝试使用SQL.

Btw, if you are not sure what you are doing, you should always try your SQL first in the MySql Workbench utility.

代码

  FDQuery1.SQL.Text := 'SELECT * FROM INFORMATION_SCHEMA.TABLES';
  FDQuery1.Open;
  FDQuery1.GetFieldNames(ListBox1.Items);

我有一个名为'MATestDB'的MySql数据库.为了获得其表中的字段(列)的列表,我将这段代码添加到TForm1.FormCreate:

I have a MySql database called 'MATestDB'. To get a list of the fields (columns) in its tables, I would add this code to TForm1.FormCreate:

  FDQuery2.SQL.Text := 'select * from information_schema.columns where table_schema = ''MATestDB''';
  FDQuery2.Open;

如果希望FDQuery2及其网格跟踪FDQuery1中的选定表,则可以使用如下代码在它们之间建立master-detail关系:

If you want FDQuery2 and its grid to track the selected table in FDQuery1, you can use code like the following to set up a master-detail relationship between them:

procedure TForm1.FormCreate(Sender: TObject);
begin
  FDQuery1.SQL.Text := 'SELECT * FROM INFORMATION_SCHEMA.TABLES';

  FDQuery2.SQL.Text := 'select table_schema, table_name, column_name, data_type, character_maximum_length, ordinal_position from information_schema.columns where table_schema = :Table_Schema and table_name = :Table_Name';
  FDQuery2.IndexFieldNames := 'table_schema;table_name;ordinal_position';
  FDQuery2.MasterFields := 'table_schema;table_name';
  FDQuery2.MasterSource := DataSource1;

  FDQuery1.Open;
  FDQuery1.GetFieldNames(ListBox1.Items);

  FDQuery2.Open;
  FDQuery2.GetFieldNames(ListBox2.Items);

end;

顺便说一句,您将无法以相同的方式获取Paradox数据库的架构信息,但是您应该能够通过Google搜索如何找到要从Paradox收集的信息.

Btw, you will not be able to get schema info for a Paradox database in the same way, but you should be able to google how to find out what information you want to gather from Paradox.

Btw#2:在删除的答案中引用的Sql中,一个问题是对DBGrid2.SelectedField.ToString的引用.如果DBGrid2从FDQuery2获取其数据,则可能是DBGrid**1**.SelectedField.ToString.如果您仍然对此有疑问,建议您在新的q中进行询问,但请确保提供了重现此问题所需的所有代码.

Btw #2: In the Sql you quoted in your deleted answer, one problem would be the reference to DBGrid2.SelectedField.ToString. If DBGrid2 gets its data from FDQuery2, then you may have meant DBGrid**1**.SelectedField.ToString. If you are still having problem with that, I suggest you ask about it in a new q, but make sure you include all the code necessary to reproduce the problem.

这篇关于显示来自Delphi(RAD Studio)的数据库结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆