我在尝试从返回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)。 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)
我使用Delphi 6和我从JustSoftwareSolution下载的驱动程序dbxopenmysql50.dll
我尝试使用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;