Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Conexión con bases de datos (https://www.clubdelphi.com/foros/forumdisplay.php?f=2)
-   -   Problema con STringGrid (https://www.clubdelphi.com/foros/showthread.php?t=48079)

enecumene 14-09-2007 17:30:23

Problema con STringGrid
 
1 Archivos Adjunto(s)
Holas de Nuevo compañeros del foro, como dice el titulo tengo un problema con stringgrid, pues es la primera vez que trabajo con ello, codigo:

Código Delphi [-]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
if Edit1.Text = '' then begin
     ShowMessage('Item no existe');
     end else begin
ZQuery1.Close;
ZQuery1.SQL.Add('Select item, codigo from items');
ZQuery1.SQL.Add(' where codigo = :cod');
ZQuery1.ParamByName('cod').Value:=Edit1.Text;
ZQuery1.Open;
 if not ZQuery1.IsEmpty then begin
  StringGrid1.Row := StringGrid1.Rowcount - 1;
  StringGrid1.Cells[0,StringGrid1.Row] := ZQuery1.Fields[0].Value;
  StringGrid1.Cells[1,StringGrid1.Row] := ZQuery1.Fields[1].Value;
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  Edit1.Clear;
  Edit1.SetFocus;
  end else
      ShowMessage('Item no existe');
    end;
  end;
 end;

cuando inserto datos al stringgrid lo hace bien y me crea una nueva linea, pero cuando trato de insertar otro datos mas me tira el siguiente error:

Cita:

---------------------------
Debugger Exception Notification
---------------------------
Project Prueba.exe raised exception class EZSQLException with message 'SQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Select item, codigo from items where codigo = '2020'' at line 1'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
que estoy haciendo mal?:confused::confused:

adjunto el codigo completo: Archivo Adjunto 994

poliburro 14-09-2007 17:44:13

Si observas, Mysql te está diciendo que el erro es por que en el where está entrando una comilla simple de más, revisa tu Edit.

enecumene 14-09-2007 17:56:49

Gracias poliburro por tu buen ojo me di cuenta que me faltaba un clear:

Código Delphi [-]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
if Edit1.Text = '' then begin
     ShowMessage('Item no existe');
     end else begin
ZQuery1.Close;
ZQuery1.SQL.Clear;<----AQUI
ZQuery1.SQL.Add('Select item, codigo from items');
ZQuery1.SQL.Add(' where codigo = :cod');
ZQuery1.ParamByName('cod').Value:=Edit1.Text;
ZQuery1.Open;
 if not ZQuery1.IsEmpty then begin
  StringGrid1.Row := StringGrid1.Rowcount - 1;
  StringGrid1.Cells[0,StringGrid1.Row] := ZQuery1.Fields[0].Value;
  StringGrid1.Cells[1,StringGrid1.Row] := ZQuery1.Fields[1].Value;
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  Edit1.Clear;
  Edit1.SetFocus;
  end else
      ShowMessage('Item no existe');
    end;
  end;
 end;

Muchas Gracias!!! :). me funciona de pelos..

Saludos.

enecumene 14-09-2007 19:58:56

siguiendo con el mismo hilo, me ocurre otro problema y es al intentar eliminar una fila especifica, aqui tengo un codigo de propiedad de seoane y que lo adapte para mis fines:

Código Delphi [-]
procedure EliminarFila(Grid: TStringGrid; Fila: Integer);
var
  i: integer;
begin
  if (Fila >= 0) and (Fila < Grid.RowCount)  then
  begin
    for i:= Fila to Grid.RowCount - 2 do
      Grid.Rows[i].Assign(Grid.Rows[i+1]);
    Grid.RowCount:= Grid.RowCount - 1;
  end;
end;

el codigo funciona al eliminar pero no elimina la fila especifica la que selecciono sino el primero. Existe una mejor solucion?

Saludos..


La franja horaria es GMT +2. Ahora son las 12:30:08.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi