Fórum Como realizar uma consulta (select) no banco de dados mysql usando o componente Zeos no delphi 7 #592293
09/04/2018
0
Desde já agradeço e aguardo resposta ;-)
Luiz Silva
Curtir tópico
+ 0Posts
09/04/2018
Ilano Frota
With SeuComponenteZeos Do
Begin
Close;
SQL.Clear;
SQL.Add('SELECT ID, NOME FROM SUA_TABELA');
If SEU_COMBOBOX.ItemIndex = 0 Then
SQL.Add('WHERE ID = ' + EDIT1.Text)
Else
If SEU_COMBOBOX.ItemIndex = 1 Then
SQL.Add('WHERE NOME = ' + QuotedStr(EDIT1.Text));
SQL.Add('ORDER BY NOME');
Open;
End;Você pode, também, antes de montar a consulta, passar as condições para uma variável, tipo:
Var
pParametro : String;
begin
If SEU_COMBOBOX.ItemIndex = 0 Then
pParametro = ' ID = ' + EDIT1.Text
Else
If SEU_COMBOBOX.ItemIndex = 1 Then
pParametro =' NOME = ' + QuotedStr(EDIT1.Text);
With SeuComponenteZeos Do
Begin
Close;
SQL.Clear;
SQL.Add('SELECT ID, NOME FROM SUA_TABELA');
If Trim(pParametro) <> '' Then SQL.Add('WHERE ' + Trim(pParametro));
SQL.Add('ORDER BY NOME');
Open;
End;
end;Outra forma seria criar um objeto e chamá-lo no seu form. Dessa forma, é muito melhor, pois facilitará sua manutenção e você poderá utilizar a mesma consulta para toda a aplicação. Por exemplo, crie uma Unit e copie o código abaixo, modificando os campos e variáveis para sua realidade:
unit _objProdutos;
interface
Uses
SysUtils, Classes, ZConnection, ZSqlProcessor, DB, ZAbstractRODataset,
ZAbstractDataset, ZDataset, UDados;
function ConsultaProduto(pQuery : TZQuery; pParametro : String; pOrdem : String) : String;
function IncluirProduto(pQuery : TZQuery; pCodigo : String; pNome : String; pObs : String) : String;
function AlteraProduto(pQuery : TZQuery; pID : Integer; pCodigo : String; pNome : String; pObs : String) : String;
implementation
function ConsultaProduto(pQuery : TZQuery; pParametro : String; pOrdem : String) : String;
begin
Try
With pQuery Do
Begin
Close;
SQL.Clear;
SQL.Add(' SELECT ID, CODIGO, NOME, OBSERVACAO ');
SQL.Add(' FROM TBPRODUTO ');
If pParametro <> '' Then SQL.Add(' WHERE ' + pParametro + ' ');
If pOrdem <> '' Then SQL.Add(' ORDER BY ' + pOrdem + '; ') Else SQL.Add(' ORDER BY CODIGO; ');
Open;
End;
Except
On E: Exception Do
Begin
ShowMessage('Ocorreu um erro ao tentar consultar registros na tabela:' + #13 + #13 + E.Message);
End;
End;
end;
function IncluirProduto(pQuery : TZQuery; pCodigo : String; pNome : String; pObs : String) : String;
begin
ConsultaProduto(pQuery, ' CODIGO = '+ QuotedStr(pCodigo), '');
Try
If pQuery.RecordCount = 0 Then
Begin
With pQuery Do
Begin
Close;
SQL.Clear;
SQL.Add(' INSERT INTO TBPRODUTO (CODIGO, NOME, OBSERVACAO) VALUE (');
SQL.Add(QuotedStr(Trim(pCodigo)) + ', ');
SQL.Add(QuotedStr(Trim(pNome)) + ', ');
If Trim(pObs) <> '' Then SQL.Add(Quoted(pObs)) Else SQL.Add('NULL');
SQL.Add(');');
ExecSQL;
End;
End
Else
If pQuery.RecordCount > 0 Then
Begin
ShowMessage('Este registro já foi cadastrado.');
End;
Except
On E: Exception Do
Begin
ShowMessage('Ocorreu um erro ao tentar incluir registros na tabela:' + #13 + #13 + E.Message);
End;
End;
end;
function AlteraProduto(pQuery : TZQuery; pID : Integer; pCodigo : String; pNome : String; pObs : String) : String;
begin
ConsultaProduto(pQuery, ' ID <> ' + IntToStr(pID) + ' AND CODIGO = '+ QuotedStr(pCodigo), '');
Try
If pQuery.RecordCount = 0 Then
Begin
With pQuery Do
Begin
Close;
SQL.Clear;
SQL.Add(' UPDATEO TBPRODUTO SET ');
SQL.Add(' CODIGO = ' + QuotedStr(Trim(pCodigo)) + ', ');
SQL.Add(' NOME = ' + QuotedStr(Trim(pNome)) + ', ');
If Trim(pObs) <> '' Then SQL.Add(' OBSERVACAO = ' + Quoted(pObs)) Else SQL.Add(' OBSERVACAO = NULL');
SQL.Add(' WHERE ID = ' + IntToStr(pID));
ExecSQL;
End;
End
Else
If pQuery.RecordCount > 0 Then
Begin
ShowMessage('Este registro já foi cadastrado.');
End;
Except
On E: Exception Do
Begin
ShowMessage('Ocorreu um erro ao tentar incluir registros na tabela:' + #13 + #13 + E.Message);
End;
End;
end;
end.
No formulário de cadastro, chame a unit logo abaixo da linha IMPLEMENTATION assim:
implementation
Uses _objProdutos;
{$R *.dfm}
E utilize a consulta no evento OnClick do botão, por exemplo, assim:
If SEU_COMBOBOX.ItemIndex = 0 Then
ConsultaProduto(SUA_QUERY_ZEOS, ' ID = ' + EDIT1.Text, 'NOME')
Else
If SEU_COMBOBOX.ItemIndex = 1 Then
ConsultaProduto(SUA_QUERY_ZEOS, ' NOME = ' + Quoted(EDIT1.Text), 'NOME');
Pronto, é isso. Agora é escolhar a melhor forma de trabalhar. Eu sugiro a terceira.
Gostei + 0
09/04/2018
Ilano Frota
Para dar certo as mensagens, lá no objeto _objProdutos altere o uses de:<br /><br />
<br /><br />
Uses<br /><br /> SysUtils, Classes, ZConnection, ZSqlProcessor, DB, ZAbstractRODataset,<br /><br /> ZAbstractDataset, ZDataset, UDados;
<br /><br />
para:<br /><br />
<br /><br />
Uses<br /><br /> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,<br /><br /> ZConnection, ZSqlProcessor, DB, ZAbstractRODataset, ZAbstractDataset, ZDataset, UDados;
<br /><br />
Outra coisa, UDados é o nome do meu DataSource, se tive criado, substitua o UDados para o nome de seu DataSource.<br /><br />
Gostei + 0
10/04/2018
Luiz Silva
Gostei + 0
10/04/2018
Ilano Frota
Cara, vai mostrar sim, desde que seu DBGrid esteja vinculado à query em questão. Sem problemas.
Gostei + 0
10/04/2018
Luiz Silva
Gostei + 0
21/04/2018
Luiz Silva
Aguardo resposta ;-)
Gostei + 0
21/04/2018
Luiz Silva
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)