Ver Mensaje Individual
  #1  
Antiguo 29-10-2006
domenor domenor is offline
Registrado
 
Registrado: oct 2006
Posts: 6
Reputación: 0
domenor Va por buen camino
Sqlite con Delphi

Encontre este ejemplo para usar sqlite con Delphi pero no estoy familiarizado con la sintaxis de sql
sin usar data sources. ¿Como puedo agregar un tercer campo llamado precio? y si no fuese mucho pedir, ¿Alguien podría comentar el código?.
Creo que a todos nos serviría saber un poco más de bases de datos empotradas.

Saludos a todos desde La Serena. Chile

Código Delphi [-]
procedure TMainForm.FormCreate(Sender: TObject);
begin
    db := TSqliteDatabase.Open('mibasedatos.db3');
    rs := TStringList.Create;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
    rs.Free;
    db.Free;
end;

procedure TMainForm.EditEnter(Sender: TObject);
begin
    Status.Font.Color := clWindowText;
    Status.Caption := 'Ingrese nombre ' + TEdit(Sender).Name;
end;

procedure TMainForm.EditExit(Sender: TObject);
begin
    if ActiveControl <> GoButton then
        TEdit(Sender).Text := '';
end;

procedure TMainForm.RealtimeClick(Sender: TObject);
begin
    GoButton.Enabled := not Realtime.Checked;
end;

procedure TMainForm.RealtimeUpdate(Sender: TObject);
begin
    if Realtime.Checked then
        GoButtonClick(Sender);
end;


procedure TMainForm.GridData(Sender: TObject; Item: TListItem);
begin
    with Item do begin
        if Index < rs.Count then begin
            Caption := rs.Names[Index];
            if SubItems.Count = 0 then
                SubItems.Add('');
            SubItems[0] := rs.ValueFromIndex[Index];
        end;
    end;
end;

procedure TMainForm.GoButtonClick(Sender: TObject);
var
    sql,field,filter: string;
begin
    if not (Sender is TEdit) then
        if descripcion.Text <> ''
            then Sender := descripcion
            else Sender := codigo;

    Grid.Clear;
    filter := Trim(TEdit(Sender).Text);
    if filter = '' then begin
        Status.Caption := '';
        Grid.Width := Grid.Width - 1;
        exit;
    end;

    field := TEdit(Sender).Name;
    sql := 'select codigo,descripcion from productos where '
        + db.Like(field, filter, field = 'descripcion');

    if LimitRecords.Checked then
        sql := sql + ' limit ' + IntToStr(Grid.VisibleRowCount);

    rs.Clear;
    with db.Query(sql) do begin
        while not Eof do begin
            rs.Add(FieldAsString(0) + '=' + FieldAsString(1));
            Next;
        end;
        Free;
    end;

    if rs.Count = 0
        then Status.Font.Color := clRed
        else Status.Font.Color := clWindowText;

    if LimitRecords.Checked and (rs.Count = Grid.VisibleRowCount)
        then Status.Caption := 'Mostrando primeros ' + IntToStr(rs.Count) + ' productos'
        else Status.Caption := IntToStr(rs.Count) + ' productos encontrados';

    Grid.Items.Count := rs.Count;
    Grid.Width := Grid.Width - 1;
end;

end.

Última edición por dec fecha: 29-10-2006 a las 11:34:42.
Responder Con Cita