2 つのフィールドを返す SQL ステートメントからフィールドを取得しようとすると問題が発生します
phpMyAdmin でクエリを実行すると、すべてのフィールドが正しく返されます。これは、SQL ステートメントが正しいことを意味します。
フィールドを 1 つだけ指定して SELECT ステートメントを実行すると、正しい情報が返されます。つまり、接続は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
これは機能します
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 は 1 つのフィールド (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
これにより、次のエラー メッセージが返されます: フィールド サイズが無効です
ノート
p.id_product is int(10)
p.id_lang is int(10)
l.name is varchar(128)
JustSoftwareSolutionからダウンロードしたドライバ dbxopenmysql50.dll で Delphi 6 を使用しています。
TSQLConnection、TSLDataSet、TSQLClientDataSet を使用してみました。これら 3 つすべてが、同じ命令で同じエラー メッセージを返します。
テスト用に簡単なプログラムを作成しました。接続して必要な情報を取得するために使用する主な機能を次に示します。
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;