[Delphi] - programowe index w tabeli Paradox

0

[Delphi] - programowe index w tabeli Paradox, kolejna odsłona

Witam.

Pisze program jak zapewne większość, którzy wchodzą na forum...
otóż pisze i napotykam problem jeden z wielu...
i tak sobie pomyślałem, ze po co się męczyć wejdę i napisze zapytanie, ktoś za mnie zrobi,
a ja skopiuje, wkleję i będzie gites... ;)

A poważnie już kolejna dobę tłokę temat, szukam po forach

http://4programmers.net/Forum/viewtopic.php?id=78935

http://www.experts-exchange.com/tag/addindex#allResults

http://delphi.cartall.com.pl/Pytania/no-frames.html
p. 61.

i wiele innych i wszystko jest na jedno kopyto.
a ja jak wpisuje

    Table1.CreateTable;
    Table1.AddIndex('NewIndex','ID;Towar',[ixUnique,ixCaseInsensitive]);

To mi wyskakuje "table is not indexed".

Dwa moduly, których uzywam.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, DBTables;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    ShowBDu1: TMenuItem;
    Session1: TSession;
    procedure FormCreate(Sender: TObject);
    procedure ShowBDu1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure DataBase_Tworz(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

uses DBu;

{$R *.dfm}

procedure TForm1.DataBase_Tworz(Sender: TObject);
begin
  Baza1Name:= 'MyBaza1';
  GetDir(0, DirMyBaza1);
  DirMyBaza1:= DirMyBaza1 + '\' + Baza1Name;

  Session.AutoSessionName:= True;

  if not DirectoryExists(DirMyBaza1)
    then CreateDir(DirMyBaza1);
  Session.AddStandardAlias(Baza1Name, DirMyBaza1, '');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.DataBase_Tworz(Sender);

end;

procedure TForm1.ShowBDu1Click(Sender: TObject);
begin
  FDB.ShowModal;
end;

end.
unit DBu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids, Menus, Mask, DBCtrls;

type
  TFDB = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    MainMenu1: TMainMenu;
    Dodaj1: TMenuItem;
    Usun1: TMenuItem;
    Edytuj1: TMenuItem;
    Zapisz1: TMenuItem;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBText1: TDBText;
    procedure Dodaj1Click(Sender: TObject);
    procedure Usun1Click(Sender: TObject);
    procedure Edytuj1Click(Sender: TObject);
    procedure Zapisz1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Tabele1_Tworz(Sender: TObject);
  end;

var
  FDB: TFDB;
  Baza1Name: ShortString;
  DirMyBaza1: ShortString;


implementation

{$R *.dfm}




procedure TFDB.Tabele1_Tworz(Sender: TObject);
begin
  Table1.DatabaseName := Baza1Name;
  Table1.TableType := ttParadox;
  Table1.TableName := 'MainTable';

  if not Table1.Exists then
  begin
    with Table1.FieldDefs do
    begin
      with AddFieldDef do begin
        Name := 'ID';            // nazwa parametru ? ID
        DataType := ftInteger;   // typ parametru ? Integer
        Required := True;        // pole jest wymagane
      end;
      with AddFieldDef do begin
        Name := 'Towar'; // nazwa parametru ? towar
        DataType := ftString; // typ parametru ? String
        Required := False;  // pole nie jest wymagane
      end;
      with AddFieldDef do begin
        Name := 'Cena'; // nazwa parametru ? cena
        DataType := ftCurrency; // typ parametru ? Currency
        Required := True;  // pole jest wymagane
      end;
      with AddFieldDef do begin
        Name := 'Data'; // nazwa parametru ? cena
        DataType := ftDateTime; // typ parametru ? DataTime
        Required := True;  // pole jest wymagane
      end;
    end;

    // utwórz tabele
    Table1.CreateTable;
    Table1.AddIndex('NewIndex','ID;Towar',[ixUnique,ixCaseInsensitive]);

     {
     With Table1.IndexDefs do begin
      Clear;
      // The 1st index has no name because it is
      // a Paradox primary key
      with AddIndexDef do begin
        Name := '';
        Fields := 'Field1';
        Options := [ixPrimary];
      end;
      with AddIndexDef do begin

        Name := 'Fld2Indx';
        Fields := 'Field2';
        Options := [ixCaseInsensitive];
      end;
    end;
    }
    {
    procedure AddIndex(const Name: string;
                       const Fields: string;
                       Options: TIndexOptions;
                       const DescFields: string = '');
    }
    {
    Table1.AddIndex('iID',
                    'ID; Towar; Cena; Data',
                    [ixCaseInsensitive],
                    'Towar; Cena; Data');
    }
    // Table1.AddIndex('iID','ID',[ixUnique,ixCaseInsensitive]);
  end;


end;
// ms-help://borland.bds5/delphivclwin32/!!MEMBERTYPE_Methods_DBTables_TTable.html


{
ftUknown	  Nieokreślony typ pola
ftString	  Łańcuch tekstowy
ftInteger	  32-bitowa liczba całkowita typu Integer
ftWord	    16-bitowa liczba typu Word
ftSmallInt  16-bitowa wartość typu SmallInt
ftFloat	    Wartość zmiennoprzecinkowa
ftBoolean	  True lub False
ftCurrency	Wartość zmiennoprzecinkowa
ftDateTime	Data i czas
ftGraphic	  Bitmapa
ftFmtMemo	  Pole Memo
ftTypedBinary	Pole binarne (typowane)
ftBlob	    Duże pole binarne
}


{
procedure TMainForm.FormCreate(Sender: TObject);
begin
  Table.Active := True;

  while not Table.Eof do
  begin
    Memo1.Lines.Add(Table.FieldValues['Towar']);
    Table.Next;
  end;
end;

procedure TMainForm.btnSaveClick(Sender: TObject);
begin
  Table.Append;
  Table.FieldValues['ID'] := 34;
  Table.FieldValues['Towar'] := 'Proszek do prania';
  Table.FieldValues['Cena'] := 2.10;
  Table.FieldValues['Data'] := Now;
  Table.Post;
end;
}


procedure TFDB.FormCreate(Sender: TObject);
begin
  // Widok
  FDB.DBGrid1.Align:= alTop;
  FDB.DBGrid1.ReadOnly:= True;

  // Do bazy danych
  FDB.Tabele1_Tworz(Sender);
  FDB.Table1.Active := True;

  FDB.DataSource1.DataSet:= FDB.Table1;
  FDB.DBGrid1.DataSource:= FDB.DataSource1;

  DBText1.DataSource:= FDB.DataSource1;
  DBText1.DataField:= 'ID';
  DBEdit1.DataSource:= FDB.DataSource1;
  DBEdit1.DataField:= 'Towar';
  DBEdit2.DataSource:= FDB.DataSource1;
  DBEdit2.DataField:= 'Cena';
  DBEdit3.DataSource:= FDB.DataSource1;
  DBEdit3.DataField:= 'Data';
end;

procedure TFDB.FormDestroy(Sender: TObject);
begin
  Table1.Active := False;
end;


procedure TFDB.Dodaj1Click(Sender: TObject);
begin//
  { dodawanie nowego rekordu }
  Table1.Append;
end;

procedure TFDB.Edytuj1Click(Sender: TObject);
begin//

end;

procedure TFDB.Usun1Click(Sender: TObject);
begin//
  { usuwanie zaznaczonej pozycji }
  Table1.Delete;
end;

procedure TFDB.Zapisz1Click(Sender: TObject);
begin//
  { akceptacja zmian }
  Table1.Post;
end;






end.
0
with Table1 do begin
  Active := False;  
  DatabaseName := 'DBDEMOS';
  TableType := ttParadox;
  TableName := 'CustInfo';

  { Don't overwrite an existing table }

  if not Table1.Exists then begin
    { The Table component must not be active }
    { First, describe the type of table and give }
    { it a name }
    { Next, describe the fields in the table }
    with FieldDefs do begin
      Clear;
      with AddFieldDef do begin
        Name := 'Field1';
        DataType := ftInteger;
        Required := True;
      end;
      with AddFieldDef do begin

        Name := 'Field2';
        DataType := ftString;
        Size := 30;
      end;
    end;
    { Next, describe any indexes }
    with IndexDefs do begin
      Clear;
      { The 1st index has no name because it is
      { a Paradox primary key }
      with AddIndexDef do begin
        Name := '';
        Fields := 'Field1';
        Options := [ixPrimary];
      end;
      with AddIndexDef do begin

        Name := 'Fld2Indx';
        Fields := 'Field2';
        Options := [ixCaseInsensitive];
      end;
    end;
    { Call the CreateTable method to create the table }
    CreateTable;
  end;
end;

zobacz tak

0

Dziękuję,
indeksy tworzy, nie zdążyłem sprawdzić jeszcze jak działają, ale to kwestia czasu i kodu.

Jeszcze raz dzięki.

1 użytkowników online, w tym zalogowanych: 0, gości: 1