Fórum Salvar e update com IBQuery #326583
01/08/2006
0
begin
with DB do
begin
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
if (Locate(´CORRETORA´,Trim(Corretora),[loCaseInsensitive])) then
begin
Active := False;
SQL.Clear;
SQL.Add(´Select * from DATA where CORRETORA=´ + #39 + Trim(Corretora) + 39);
Active := True;
Edit;
DecimalSeparator := ´.´;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
Post;
end
else
begin
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
Insert;
DecimalSeparator := ´.´;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
Post;
end;
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
end;
end;
estou tentando atualizar o field caso ja exista a corretora com o nome indicado, caso contrario ele salvar um novo registro..
agradeço qm poder me ajudar
Nightshade
Curtir tópico
+ 0Posts
01/08/2006
Renato.pavan
Da alguma mensagem de erro?
Caso sim, qual mensagem?
Renato.
Gostei + 0
02/08/2006
Nightshade
begin
with DB do
begin
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
if (FieldByName(´DIA´).AsString = Dia) then
begin
if (Locate(´CORRETORA´,Trim(Corretora),[loCaseInsensitive,loPartialKey])) then
begin
Active := False;
SQL.Clear;
SQL.Add(´Select * from DATA where CORRETORA=´ + #39 + Trim(Corretora) + 39);
Active := True;
Edit;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
DecimalSeparator := ´.´;
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_COMPRAS´).AsFloat := StrToFloat(MCompras);
DecimalSeparator := ´.´;
FieldByName(´VENDAS´).AsFloat := StrToFloat(Vendas);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_VENDAS´).AsFloat := StrToFloat(MVendas);
DecimalSeparator := ´.´;
FieldByName(´POSICAO_LIQUIDA´).AsFloat := StrToFloat(PLiquida);
DecimalSeparator := ´,´;
FieldByName(´MEDIA_LIQUIDA´).AsFloat := StrToFloat(MLiquida);
FieldByName(´PERC_VENDAS´).AsFloat := StrToFloat(PVendas);
FieldByName(´PERC_COMPRAS´).AsFloat := StrToFloat(PCompras);
FieldByName(´HORA´).AsString := Hora;
FieldByName(´DIA´).AsString := Dia;
Post;
end;
end
else
begin
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
Insert;
DecimalSeparator := ´.´;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_COMPRAS´).AsFloat := StrToFloat(MCompras);
DecimalSeparator := ´.´;
FieldByName(´VENDAS´).AsFloat := StrToFloat(Vendas);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_VENDAS´).AsFloat := StrToFloat(MVendas);
DecimalSeparator := ´.´;
FieldByName(´POSICAO_LIQUIDA´).AsFloat := StrToFloat(PLiquida);
DecimalSeparator := ´,´;
FieldByName(´MEDIA_LIQUIDA´).AsFloat := StrToFloat(MLiquida);
FieldByName(´PERC_VENDAS´).AsFloat := StrToFloat(PVendas);
FieldByName(´PERC_COMPRAS´).AsFloat := StrToFloat(PCompras);
FieldByName(´HORA´).AsString := Hora;
FieldByName(´DIA´).AsString := Dia;
Post;
end;
Active := False;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Active := True;
end;
end;
Porem eu gostaria de criar um novo record apenas se o dia ou a corretora for diferente..
caso contrario dar update nele
Gostei + 0
02/08/2006
Renato.pavan
procedure SaveData(DB: TIBQuery; Corretora,Compras,MCompras,Vendas,MVendas,PLiquida,MLiquida,PCompras,PVendas,Dia,Hora: String);
begin
with DB do
begin
Close;
SQL.Clear;
{ *** Monto a sql passando por parametro o dia e a corretora *** }
SQL.Add(´SELECT * FROM DATA WHERE DIA = :DIA AND CORRETORA = :CORRETORA´);
ParamByName(´DIA´).AsDate := StrToDate(Dia); //Passo o dia por parametro
ParamByName(´CORRETORA´).AsString := Corretora; //Passo a corretora por parametro
Open; //Abro a consulta
{ *** Verifico se a tabela não esta vazia - not (IsEmpty) ***}
if not (IsEmpty) then { *** Se não funcionar if not vc testa if RecordCount = 0 then *** }
begin
{ *** Caso não esteja vazia *** }
Edit;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
DecimalSeparator := ´.´;
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_COMPRAS´).AsFloat := StrToFloat(MCompras);
DecimalSeparator := ´.´;
FieldByName(´VENDAS´).AsFloat := StrToFloat(Vendas);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_VENDAS´).AsFloat := StrToFloat(MVendas);
DecimalSeparator := ´.´;
FieldByName(´POSICAO_LIQUIDA´).AsFloat := StrToFloat(PLiquida);
DecimalSeparator := ´,´;
FieldByName(´MEDIA_LIQUIDA´).AsFloat := StrToFloat(MLiquida);
FieldByName(´PERC_VENDAS´).AsFloat := StrToFloat(PVendas);
FieldByName(´PERC_COMPRAS´).AsFloat := StrToFloat(PCompras);
FieldByName(´HORA´).AsString := Hora;
FieldByName(´DIA´).AsString := Dia;
Post;
end
else
begin
{ *** Caso esteja vazia *** }
Append;
DecimalSeparator := ´.´;
FieldByName(´CORRETORA´).AsString := Trim(Corretora);
FieldByName(´COMPRAS´).AsFloat := StrToFloat(Compras);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_COMPRAS´).AsFloat := StrToFloat(MCompras);
DecimalSeparator := ´.´;
FieldByName(´VENDAS´).AsFloat := StrToFloat(Vendas);
DecimalSeparator := ´,´;
FieldByName(´MEDIO_VENDAS´).AsFloat := StrToFloat(MVendas);
DecimalSeparator := ´.´;
FieldByName(´POSICAO_LIQUIDA´).AsFloat := StrToFloat(PLiquida);
DecimalSeparator := ´,´;
FieldByName(´MEDIA_LIQUIDA´).AsFloat := StrToFloat(MLiquida);
FieldByName(´PERC_VENDAS´).AsFloat := StrToFloat(PVendas);
FieldByName(´PERC_COMPRAS´).AsFloat := StrToFloat(PCompras);
FieldByName(´HORA´).AsString := Hora;
FieldByName(´DIA´).AsString := Dia;
Post;
end;
Close;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Open;
end;
end;Uma dica, ao invés de usar edit/post append/post vc poderia fazer isso direto com instrucao sql - update tabela set campo = valor / insert into (campo1, campo2) values (valor1, valor2) from tabela
Gostei + 0
03/08/2006
Nightshade
e como estruturaria esse codigo com o insert e update?
eu havia tentado antes dessa forma mas dava erro, nao exibia na grid.
eu nao lembro se estava abrindo um novo select apos o comando..
Gostei + 0
03/08/2006
Nightshade
e como estruturaria esse codigo com o insert e update?
eu havia tentado antes dessa forma mas dava erro, nao exibia na grid.
eu nao lembro se estava abrindo um novo select apos o comando...
Gostei + 0
03/08/2006
Renato.pavan
e como estruturaria esse codigo com o insert e update?
eu havia tentado antes dessa forma mas dava erro, nao exibia na grid.
eu nao lembro se estava abrindo um novo select apos o comando...[/quote:3ec2e6b2bf]
Me passa a estrutura completa das tabelas e qual banco vc usa q monto isso pra vc.
[]´s
Renato
Gostei + 0
08/08/2006
Nightshade
begin
with DB do
begin
Close;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA WHERE DIA = :DIA AND CORRETORA = :CORRETORA´);
ParamByName(´DIA´).AsDate := StrToDate(Dia);
ParamByName(´CORRETORA´).AsString := Corretora;
Open;
if not (IsEmpty) then
begin
Close;
SQL.Clear;
SQL.Add(´UPDATE DATA SET ´ +
´CORRETORA=´ + #39 + Corretora + 39 +
´ COMPRAS=´ + 39 + Compras + 39 +
´ MEDIO_COMPRAS=´ + 39 + MCompras + 39 +
´ VENDAS=´ + 39 + Vendas + 39 +
´ MEDIO_VENDAS=´ + 39 + MVendas + 39 +
´ POSICAO_LIQUIDA=´ + 39 + PLiquida + 39 +
´ MEDIA_LIQUIDA=´ + 39 + MLiquida + 39 +
´ PERC_VENDAS=´ + 39 + PVendas + 39 +
´ HORA=´ + #39 + Hora + 39 +
´ DIA=´ + 39 + Dia + 39 +
´ LEITURA=´ + 39 + FOptions.ComboBox1.Text + 39 +
´ WHERE DIA=´ + 39 + Dia + 39 + ´ AND´ +
´ CORRETORA=´ + 39 + Corretora + 39);
Open;
end
else
begin
Close;
SQL.Clear;
SQL.Add(´INSERT INTO DATA (CORRETORA,COMPRAS,MEDIO_COMPRAS,VENDAS,´ +
´MEDIO_VENDAS,POSICAO_LIQUIDA,MEDIA_LIQUIDA,PERC_VENDAS,HORA,DIA,LEITURA)´ +
´ VALUES (´ + 39 + Corretora + 39 + ´,´ +
39 + Compras + 39 + ´,´ + 39 + MCompras + 39 + ´,´ + 39 + Vendas + 39 + ´,´ +
39 + MVendas + 39 + ´,´ + 39 + PLiquida + 39 + ´,´ + 39 + MLiquida + 39 + ´,´ +
39 + PCompras + 39 + ´,´ + 39 + PVendas + 39 + ´,´ + 39 + Dia + 39 + ´,´ +
39 + Hora + 39 + ´)´);
Open;
end;
Close;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Open;
end;
end;
Gostei + 0
08/08/2006
Nightshade
Var
Leitura: String;
begin
Leitura := ´TESTE´;
with DB do
begin
Close;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA WHERE DIA = :DIA AND CORRETORA = :CORRETORA´);
ParamByName(´DIA´).AsDate := StrToDate(Dia);
ParamByName(´CORRETORA´).AsString := Corretora;
Open;
if not (IsEmpty) then
begin
Close;
SQL.Clear;
SQL.Add(´UPDATE DATA SET ´ +
´CORRETORA=´ + #39 + Corretora + 39 +
´ ,COMPRAS=´ + Compras +
´ ,MEDIO_COMPRAS=´ + MCompras +
´ ,PERC_COMPRAS=´ + PCompras +
´ ,VENDAS=´ + Vendas +
´ ,MEDIO_VENDAS=´ + MVendas +
´ ,PERC_VENDAS=´ + PVendas +
´ ,POSICAO_LIQUIDA=´ + PLiquida +
´ ,MEDIA_LIQUIDA=´ + MLiquida +
´ ,DIA=´ + #39 + Dia + 39 +
´ ,HORA=´ + 39 + Hora + 39 +
´ ,LEITURA=´ + 39 + Leitura + 39 +
´ WHERE DIA=´ + 39 + Dia + 39 + ´ AND´ +
´ CORRETORA=´ + 39 + Corretora + 39);
Open;
end
else
begin
Close;
SQL.Clear;
SQL.Add(´INSERT INTO DATA (CORRETORA,COMPRAS,MEDIO_COMPRAS,PERC_COMPRAS,´ +
´VENDAS,MEDIO_VENDAS,PERC_VENDAS,POSICAO_LIQUIDA,MEDIA_LIQUIDA,DIA,HORA,LEITURA)´ +
´ VALUES (´ + 39 + Corretora + 39 + ´,´ + Compras + ´,´ + MCompras + ´,´ + PCompras +
´,´ + Vendas + ´,´ + MVendas + ´,´ + PVendas + ´,´ + PLiquida + ´,´ + MLiquida + ´,´ +
39 + Dia + 39 + ´,´ + 39 + Hora + 39 + ´,´ + 39 + Leitura + 39 + ´)´);
Open;
end;
Close;
SQL.Clear;
SQL.Add(´SELECT * FROM DATA ORDER BY CORRETORA´);
Open;
end;
end;
//-------- VLW Pelo tok renato.pavan
eu tv usando ´´ pra campo integer e nem me dei d conta AhuAHuHAhuA
flw
Gostei + 0