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)
-   -   Sqlite con Delphi (https://www.clubdelphi.com/foros/showthread.php?t=36949)

domenor 29-10-2006 02:57:53

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.


La franja horaria es GMT +2. Ahora son las 03:30:45.

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