SQL 仅显示一个字段

发布于 2024-11-14 13:56:38 字数 5128 浏览 6 评论 0 原文

我在尝试从返回 2 个字段的 SQL 语句获取字段时遇到问题

如果我在 phpMyAdmin 中运行查询,它会正确返回所有字段。 这意味着 SQL 语句是正确的

如果我运行仅包含一个字段的 SELECT 语句,它会返回正确的信息。 这意味着连接正常

select p.id_product from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

这有效

select p.id_product, l.id_lang from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

这也有效,bu 仅返回一个字段 (id_product)。字段数=1!

select p.id_product, l.name from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

这将返回此错误消息: 字段大小无效

注意,

p.id_product is int(10)
p.id_lang is int(10)
l.name is varchar(128)

我使用 Delphi 6 以及从 JustSoftwareSolution

我尝试使用 TSQLConnection、TSLDataSet 和 TSQLClientDataSet。它们三个都在同一条指令中返回相同的错误消息:Componente.Open;

我做了一个简单的程序进行测试。以下是我用来连接和获取所需信息的主要功能。


procedure TFMMain.EstableceConexionMySQL;
var
  oIni : TiniFile;
begin
  //Conecto si no está conectada
  with MYSQLConnection do
  begin
     try
        if (not Connected) then
        begin
           oIni := TInifile.Create('G2k2Plus.ini');
           try
              DriverName := 'dbxmysql';
              GetDriverFunc := 'getSQLDriverMYSQL50';
              LibraryName := 'dbxopenmysql50.dll';
              VendorLib := 'libmysql.dll';
              LoginPrompt := False;
              Params.Clear;
              Params.Append('BlobSize=-1');
              Params.Append('ErrorResourceFile=');
              Params.Append('LocaleCode=0000');
              Params.Append('Database=' + oIni.ReadString('TiendaVirtual', 'Database ', ''));
              Params.Append('User_Name=' + oIni.ReadString('TiendaVirtual', 'User_Name ', ''));
              Params.Append('Password=' + oIni.ReadString('TiendaVirtual', 'Password ', ''));
              Params.Append('HostName=' + oIni.ReadString('TiendaVirtual', 'HostName ', ''));
           finally
              oIni.Free;
           end;
           Open;
        end;
     except
        on e: Exception do
        begin
           MOutput.Lines.Add('Error al abrir conexion MySQL');
           MOutput.Lines.Add(e.Message);
        end;
     end;
  end;
end;

procedure TFMMain.BTraerDatosSQLQueryClick(Sender: TObject);
var
  Q : TSQLQuery;
  i : integer;
  Desde, Hasta : integer;
  s : string;
begin
  Desde := 0;
  Hasta := 24;
  BConectar.Click;

     if (MYSQLConnection.Connected) then
     begin
        Q := TSQLQuery.Create(nil);
        try
           with Q do
           begin
              try
                 SQLConnection := MYSQLConnection;
                 if (Active) then
                    Close;
                 SQL.Text := 'select p.id_product, l.name from ps_product p ' +
                    'left outer join ps_product_lang l ' +
                    'on p.id_product = l.id_product ' +
                    'where p.id_product >= :desde1 and p.id_product <= :hasta1';
                 //PrepareStatement;
                 Params.FindParam('desde1').Value := Desde;
                 Params.FindParam('hasta1').Value := Hasta;
                 Open; // ERROR HERE !!!
                 MOutput.Lines.Add('Campos: ' + IntToStr(Fields.Count));
                 for i := 0 to Fields.Count -1 do
                 begin
                    MOutput.Lines.Add('   DisplayName '+Fields[i].DisplayName);
                    MOutput.Lines.Add('   FullName '+Fields[i].FullName);
                    MOutput.Lines.Add('   FieldName '+Fields[i].FieldName);
                    MOutput.Lines.Add('   Origin '+Fields[i].Origin);
                 end;
                 MOutput.Lines.Add('-----------');

                 s := '';
                 for i := 0 to Fields.Count -1 do
                    s := s + UpperCase(Fields[i].FieldName)+', ';
                 MOutput.Lines.Add(s);
                 while (not EOF) do
                 begin
                    s := '';
                    for i := 0 to Fields.Count -1 do
                       s := s + Fields[i].AsString+', ';
                    MOutput.Lines.Add(s);
                    Next;
                 end;
                 MOutput.Lines.Add('-----------');
                 Close;
              except
                 on e: Exception do
                 begin
                    MOutput.Lines.Add('-----------');
                    MOutput.Lines.Add('EXCEPTION');
                    MOutput.Lines.Add(e.Message);
                 end;
              end;
           end;
        finally
           Q.Free;
        end;
     end;
end;

I have a problem trying to get fields from a SQL statement that retuns 2 fields

If I run the query in phpMyAdmin, it returns all the fields correctly. That means SQL statement is correct

If I run the SELECT statment with only one field, it returns correct information. That means connection is Ok

select p.id_product from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

This works

select p.id_product, l.id_lang from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

This also works, bu returns only one field (id_product). Fields.Count=1 !!!

select p.id_product, l.name from ps_product p 
left outer join ps_product_lang l 
on p.id_product = l.id_product 
where p.id_product >= :desde1 and p.id_product <= :hasta1

This returns this error message:
Invalid field size

Note

p.id_product is int(10)
p.id_lang is int(10)
l.name is varchar(128)

I use Delphi 6 with the driver dbxopenmysql50.dll that I downloaded from JustSoftwareSolution

I tryed using TSQLConnection, TSLDataSet y TSQLClientDataSet. all three of them return the same error message in the same instruction: Componente.Open;

I did a simple program for testing. Here are the main functions I use to connect and to get the information I need.


procedure TFMMain.EstableceConexionMySQL;
var
  oIni : TiniFile;
begin
  //Conecto si no está conectada
  with MYSQLConnection do
  begin
     try
        if (not Connected) then
        begin
           oIni := TInifile.Create('G2k2Plus.ini');
           try
              DriverName := 'dbxmysql';
              GetDriverFunc := 'getSQLDriverMYSQL50';
              LibraryName := 'dbxopenmysql50.dll';
              VendorLib := 'libmysql.dll';
              LoginPrompt := False;
              Params.Clear;
              Params.Append('BlobSize=-1');
              Params.Append('ErrorResourceFile=');
              Params.Append('LocaleCode=0000');
              Params.Append('Database=' + oIni.ReadString('TiendaVirtual', 'Database ', ''));
              Params.Append('User_Name=' + oIni.ReadString('TiendaVirtual', 'User_Name ', ''));
              Params.Append('Password=' + oIni.ReadString('TiendaVirtual', 'Password ', ''));
              Params.Append('HostName=' + oIni.ReadString('TiendaVirtual', 'HostName ', ''));
           finally
              oIni.Free;
           end;
           Open;
        end;
     except
        on e: Exception do
        begin
           MOutput.Lines.Add('Error al abrir conexion MySQL');
           MOutput.Lines.Add(e.Message);
        end;
     end;
  end;
end;

procedure TFMMain.BTraerDatosSQLQueryClick(Sender: TObject);
var
  Q : TSQLQuery;
  i : integer;
  Desde, Hasta : integer;
  s : string;
begin
  Desde := 0;
  Hasta := 24;
  BConectar.Click;

     if (MYSQLConnection.Connected) then
     begin
        Q := TSQLQuery.Create(nil);
        try
           with Q do
           begin
              try
                 SQLConnection := MYSQLConnection;
                 if (Active) then
                    Close;
                 SQL.Text := 'select p.id_product, l.name from ps_product p ' +
                    'left outer join ps_product_lang l ' +
                    'on p.id_product = l.id_product ' +
                    'where p.id_product >= :desde1 and p.id_product <= :hasta1';
                 //PrepareStatement;
                 Params.FindParam('desde1').Value := Desde;
                 Params.FindParam('hasta1').Value := Hasta;
                 Open; // ERROR HERE !!!
                 MOutput.Lines.Add('Campos: ' + IntToStr(Fields.Count));
                 for i := 0 to Fields.Count -1 do
                 begin
                    MOutput.Lines.Add('   DisplayName '+Fields[i].DisplayName);
                    MOutput.Lines.Add('   FullName '+Fields[i].FullName);
                    MOutput.Lines.Add('   FieldName '+Fields[i].FieldName);
                    MOutput.Lines.Add('   Origin '+Fields[i].Origin);
                 end;
                 MOutput.Lines.Add('-----------');

                 s := '';
                 for i := 0 to Fields.Count -1 do
                    s := s + UpperCase(Fields[i].FieldName)+', ';
                 MOutput.Lines.Add(s);
                 while (not EOF) do
                 begin
                    s := '';
                    for i := 0 to Fields.Count -1 do
                       s := s + Fields[i].AsString+', ';
                    MOutput.Lines.Add(s);
                    Next;
                 end;
                 MOutput.Lines.Add('-----------');
                 Close;
              except
                 on e: Exception do
                 begin
                    MOutput.Lines.Add('-----------');
                    MOutput.Lines.Add('EXCEPTION');
                    MOutput.Lines.Add(e.Message);
                 end;
              end;
           end;
        finally
           Q.Free;
        end;
     end;
end;

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

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

发布评论

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

评论(1

清风挽心 2024-11-21 13:56:38

正如您所看到的,我的类似问题 ,解决方案是更改 MySQL 的 dbExpress 驱动程序。 Dll dbxopenmysql50.dll 可能仅适用于 MySQL v5.0。在您的情况下,DevArt dbExpress Driver for OracleFirebird 可以使用。

As you can see to my similar problem, solution was to change dbExpress Driver for MySQL. Dll dbxopenmysql50.dll might work right only with MySQL v5.0. In your case DevArt dbExpress Driver for Oracle and Firebird could be used.

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