Fórum consulta delphi mysql #365099

15/10/2008

0

Tenho a consulta abaixo (uso Delphi 7 e MySQL 5 ) mas quando compilo dá o erro que está logo a seguir

begin
MyQuery1.Active := False;
MyQuery1.SQL.Clear;
MyQuery1.sql.Clear;
MyQuery1.SQL.Add(´SELECT * from bpa,´);
MyQuery1.sql.add(´where data >= :data1 and data <= :data2´);
MyQuery1.SQL.add(´ORDER BY data desc´);
MyQuery1.Params[0].Value := FormatDateTime(´YYYY/MM/DD´, StrToDate(Edit1.Text));
MyQuery1.Params[1].Value := FormatDateTime(´YYYY/MM/DD´, StrToDate(Edit2.Text));
MyQuery1.Prepared;
MyQuery1.Active:=true;
ppReport1.Print;
end;


---------------------------
Debugger Exception Notification
---------------------------
Project HosPlus.exe raised exception class EConvertError with message ´´01/10/2008´ is not a valid integer value´. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------


Boavida

Boavida

Responder

Posts

15/10/2008

Maikel

Não sei se vai resolver seu problema, mas tenta formartar a data usando ponto, assim:

MyQuery1.Params[0].Value := FormatDateTime(´YYYY.MM.DD´, StrToDate(Edit1.Text));
MyQuery1.Params[1].Value := FormatDateTime(´YYYY.MM.DD´, StrToDate(Edit2.Text));

Ou senão, tenta ainda mudar o formato por ´MM/dd/yyyy´ ou por ´dd/MM/yyyy´.

Não conheço MySQL muito a fundo, mas no Firebird e SQL Server é preciso ter estes cuidados.

Att


Responder

Gostei + 0

15/10/2008

Boavida

Amigão fiz as alterações que voce sugeriu e o erro permanece

Debugger Exception Notification
---------------------------
Project HosPlus.exe raised exception class EConvertError with message ´´01/10/2008´ is not a valid integer value´. Process stopped. Use Step or Run to continue.
---------------------------
OK Help

Engraçado é que no MySQL ele aceita a seguinte consulta e dá certo:

select * from bpa
where data between ´2008/10/14´ and ´2008/10/20´


Responder

Gostei + 0

15/10/2008

Boavida

realmente é um mistério

fiz agora de outra forma, usando um componente de data da paleta IPCONTROL do InfoPower

procedure TFRelBPA.Button1Click(Sender: TObject);
begin
if (wwDBDateTimePicker2.Date) < (wwDBDateTimePicker1.Date) Then
begin
ShowMessage(´ Data inválida! Informe a data correta ´);
wwDBDateTimePicker2 := wwDBDateTimePicker1;
end
Else
begin
MyQuery1.Active := False;
MyQuery1.SQL.Clear;
MyQuery1.SQL.Add(´SELECT * from bpa,´);
MyQuery1.sql.add(´where data between :data1 and :data2´);
MyQuery1.SQL.add(´ORDER BY data desc´);
MyQuery1.Params[0].Value := FormatDatetime(´YYYY/MM/DD´,wwDBDateTimePicker1.date);
MyQuery1.Params[1].value := FormatDatetime(´YYYY/MM/DD´,wwDBDateTimePicker2.date);
MyQuery1.Open;
MyQuery1.Active:=true;
ppReport1.Print;
end;
end;


teoricamente era pra dar certo pois a consulta ta igual a que da certo no Mysql (data invertida) o sistema roda e quando insiro as datas pra consultar que teclo ok, da o seguinte erro

---------------------------
Debugger Exception Notification
---------------------------
Project HosPlus.exe raised exception class EMySqlException with message ´
#42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ´WHERE data between ´2008/10/10´ AND ´2008/10/15´
ORDER BY data DESC´ at line 2´. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------


Responder

Gostei + 0

16/10/2008

Maikel

Olá

Mas agora a mensagem de erro retornada já está diferente como a anterior, tente fazer mais o seguinte teste:
-> Deixe um espaço no final de cada linha, para que a concatenação das SQLs fique com espaço entre os comandos, e aquele virgula após o nome da tabela, tem necessidade dela? Tente assim:
MyQuery1.SQL.Add(´SELECT * from bpa                   ´);
MyQuery1.sql.add(´where data between :data1 and :data2  ´);
MyQuery1.SQL.add(´ORDER BY data desc                          ´); 


E no mais, deixe mesmo como está, faz o teste e post o resultado.

Att


Responder

Gostei + 0

16/10/2008

Boavida

Amigão consegui, pois no é que era a bendita vírgula mesmo, veja o código correto

MyQuery1.Active := False;
MyQuery1.SQL.Clear;
MyQuery1.SQL.Add(´SELECT * from bpa´);
MyQuery1.sql.add(´where data between :data1 and :data2´);
MyQuery1.SQL.add(´ORDER BY data desc´);
MyQuery1.Params[0].Value := FormatDatetime(´YYYY.mm.dd´,wwDBDateTimePicker1.Date);
MyQuery1.Params[1].Value := FormatDatetime(´YYYY.mm.dd´,wwDBDateTimePicker2.Date);
MyQuery1.Open;
MyQuery1.Active:=true;
ppReport1.Print;

obrigado e abraço


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar