How can I get the table description (fields and types) from Firebird with dbExpress

后端 未结 4 1819
你的背包
你的背包 2021-02-04 07:42

I have written a tool for displaying database structures using the GetTableNames and GetFieldNames methods of TSQLConnection. How can I get the types of each field name similar

4条回答
  •  渐次进展
    2021-02-04 08:13

    Using the link which TLama provided, I found my own solution, which is somewhat similar to the above solutions, but simpler.

    SELECT R.RDB$FIELD_NAME AS field_name,
    CASE F.RDB$FIELD_TYPE
     WHEN 7 THEN 'SMALLINT'
     WHEN 8 THEN 'INTEGER'
     WHEN 9 THEN 'QUAD'
     WHEN 10 THEN 'FLOAT'
     WHEN 11 THEN 'D_FLOAT'
     WHEN 12 THEN 'DATE'
     WHEN 13 THEN 'TIME'     
     WHEN 14 THEN 'CHAR'
     WHEN 16 THEN 'INT64'
     WHEN 27 THEN 'DOUBLE'
     WHEN 35 THEN 'TIMESTAMP'
     WHEN 37 THEN 'VARCHAR'
     WHEN 40 THEN 'CSTRING'
     WHEN 261 THEN 'BLOB'
     ELSE 'UNKNOWN'
    END AS field_type,
    F.RDB$FIELD_LENGTH AS field_length,
    CSET.RDB$CHARACTER_SET_NAME AS field_charset
    FROM RDB$RELATION_FIELDS R
    LEFT JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME
    LEFT JOIN RDB$CHARACTER_SETS CSET ON F.RDB$CHARACTER_SET_ID = CSET.RDB$CHARACTER_SET_ID
    WHERE R.RDB$RELATION_NAME= :p1
    ORDER BY R.RDB$FIELD_POSITION
    

    p1 is the table name which is passed as a parameter to the query.

    In context, I have a treeview which has as its nodes the table names of a given database; for each node, the child nodes are the fields along with their definitions.

     sqlcon.GetTableNames (dbTables);    // sqlcon is the TSQLConnection
     tv.items.Clear;
     for i:= 1 to dbTables.count do
      begin
       node:= tv.items.Add (nil, dbTables[i - 1]);
       with qFields do                   // the above query
        begin
         params[0].asstring:= dbTables[i - 1];
         open;
         while not eof do
          begin
           tv.items.addchild (node, trim (fieldbyname ('field_name').asstring) + ', ' +
                                    trim (fieldbyname ('field_type').asstring) + ', ' +
                                    fieldbyname ('field_length').asstring + ', ' +
                                    fieldbyname ('field_charset').asstring);
           next
          end;
         close
        end
      end;
    

    Here is a screenshot of the program in action. I realise that the format is not the same as the DDL which I quoted, but it's obvious what each field means (at least to me, and this is a program for my private use).

    enter image description here

提交回复
热议问题