Newsgroup: it.comp.lang.delphi
Titolo: Iniziare a lavorare con i DB

Chi può darmi qualche info su come inizializzare un Database? Sto cercando
di creare una tabella ed utilizzarla all'interno di una piccola
applicazione, ma data la mia scarsa preparazione in merito, al massimo
ottengo dei blocchi di sistema (preciso che sto lavorando con Delphi 5 pro).
Perdonate la domanda, alla quale mi rendo conto benissimo non è possibile
dare una risposta esaustiva; infatti vi chiedo solo un piccolo spunto
iniziale per far funzionare una tabellina con 3 voci... tutto qui.
VI ringrazio anticipatamente, spero poter ricambiare la cortesia!
Guglielmo S.

Dimenticavo....
Si usa cosi
Procedure InitStructure(aOwner : TComponent; StartPath : String);
Begin
  If Not DirectoryExists(StartPath+'\Catalog') Then
  Begin
    MkDir(StartPath+'\Catalog');
  End;
  If Not FileExists(StartPath+'\Catalog\Category.DB') Then
  Begin
    OpenTable ( aOwner,
                'Category',
                StartPath+'\Catalog',
                'ID',
                ttParadox,
                [GetFieldDefRec ( 'Name', ftString, 30, true)],
                [GetIndexDefRec ( 'IdxName', 'Name', [ixUnique,
ixCaseInsensitive])],
                False);
  End;
  If Not FileExists(StartPath+'\Catalog\Ger.DB') Then
  Begin
    OpenTable ( aOwner,
                'Ger',
                StartPath+'\Catalog',
                'ID',
                ttParadox,
                [GetFieldDefRec ( 'ParentID', ftInteger , 0, true),
                 GetFieldDefRec ( 'ChildID', ftInteger , 0, true)],
                [GetIndexDefRec ( 'IdxParent', 'ParentID', [ixUnique,
ixCaseInsensitive])],
                False);
  End;
End;

 ha scritto nel messaggio
news:8l2cmp$oja$3...@nslave1.tin.it...

Ciao, tempo fà ho scritto questa unit che forse ti può aiutare, è un po
bucolica in alcune sue parti se vuoi migliorala, ciao!
Unit DBUtils;
Interface
Uses DBTables,Db,Classes,SysUtils;
Type
  TFieldDefRec                       = Record
    Name                             : String;
    DataType                         : TFieldType;
    Size                             : Word;
    Required                         : Boolean;
  End;
  TIndexDefRec                       = Record
    Name                             ,
    Fields                           : String;
    Options                          : TIndexOptions;
  End;
Function GetFieldDefRec ( aName      : String;
                          aDataType  : TFieldType;
                          aSize      : Word;
                          aRequired  : Boolean): TFieldDefRec;
Function GetIndexDefRec ( aName      ,
                          aFields    : String;
                          aOptions   : TIndexOptions):TIndexDefRec;
Function OpenTable   ( aOwner        : TComponent;
                       sTableName    ,
                       sDataBaseName ,
                       sAutoIdName   : String;
                       aTableType    : TTableType;
                       aFieldsDefs   : Array of TFieldDefRec;
                       aIndexDefs    : Array of TIndexDefRec;
                       bCreateActive : Boolean ): TTable;
Function GetValidPath ( aPath : String; bWantSlash : Boolean ) : String;
Function FieldTypeToInt ( aFieldType : TFieldType ) : Integer;
Function IntToFieldType ( aFieldType : Integer ) : TFieldType;
Function FieldTypeNameFromType ( aFieldType : TFieldType ) : String;
Function FieldTypeNameFromInt ( aFieldType : Integer ) : String;
Implementation
Function FieldTypeToInt ( aFieldType : TFieldType ) : Integer;
Begin
  Result:=-1;
  Case aFieldType Of
    ftUnknown     : Result:=0;
    ftString      : Result:=1;
    ftSmallint    : Result:=2;
    ftInteger     : Result:=3;
    ftWord        : Result:=4;
    ftBoolean     : Result:=5;
    ftFloat       : Result:=6;
    ftCurrency    : Result:=7;
    ftBCD         : Result:=8;
    ftDate        : Result:=9;
    ftTime        : Result:=10;
    ftDateTime    : Result:=11;
    ftBytes       : Result:=12;
    ftVarBytes    : Result:=13;
    ftAutoInc     : Result:=14;
    ftBlob        : Result:=15;
    ftMemo        : Result:=16;
    ftGraphic     : Result:=17;
    ftFmtMemo     : Result:=18;
    ftParadoxOle  : Result:=19;
    ftDBaseOle    : Result:=20;
    ftTypedBinary : Result:=21;
    ftCursor      : Result:=22;
  End;
End;
Function IntToFieldType ( aFieldType : Integer ) : TFieldType;
Begin
  Result:=ftUnknown;
  Case aFieldType Of
    0  : Result:=ftUnknown;
    1  : Result:=ftString;
    2  : Result:=ftSmallint;
    3  : Result:=ftInteger;
    4  : Result:=ftWord;
    5  : Result:=ftBoolean;
    6  : Result:=ftFloat;
    7  : Result:=ftCurrency;
    8  : Result:=ftBCD;
    9  : Result:=ftDate;
    10 : Result:=ftTime;
    11 : Result:=ftDateTime;
    12 : Result:=ftBytes;
    13 : Result:=ftVarBytes;
    14 : Result:=ftAutoInc;
    15 : Result:=ftBlob;
    16 : Result:=ftMemo;
    17 : Result:=ftGraphic;
    18 : Result:=ftFmtMemo;
    19 : Result:=ftParadoxOle;
    20 : Result:=ftDBaseOle;
    21 : Result:=ftTypedBinary;
    22 : Result:=ftCursor;
  End;
End;
Function FieldTypeNameFromType ( aFieldType : TFieldType ) : String;
Begin
  Result:='ftUnknown';
  Case aFieldType Of
    ftUnknown   : Result:='ftUnknown';
    ftString    : Result:='ftString';
    ftSmallint  : Result:='ftSmallint';
    ftInteger   : Result:='ftInteger';
    ftWord      : Result:='ftWord';
    ftBoolean   : Result:='ftBoolean';
    ftFloat     : Result:='ftFloat';
    ftCurrency  : Result:='ftCurrency';
    ftBCD       : Result:='ftBCD';
    ftDate      : Result:='ftDate';
    ftTime      : Result:='ftTime';
    ftDateTime  : Result:='ftDateTime';
    ftBytes     : Result:='ftBytes';
    ftVarBytes  : Result:='ftVarBytes';
    ftAutoInc   : Result:='ftAutoInc';
    ftBlob      : Result:='ftBlob';
    ftMemo      : Result:='ftMemo';
    ftGraphic   : Result:='ftGraphic';
    ftFmtMemo   : Result:='ftFmtMemo';
    ftParadoxOle: Result:='ftParadoxOle';
    ftDBaseOle  : Result:='ftDBaseOle';
    ftTypedBinary : Result:='ftTypedBinary';
    ftCursor    : Result:='ftCursor';
  End;
End;
Function FieldTypeNameFromInt ( aFieldType : Integer ) : String;
Begin
  Result:='ftUnknown';
  Case aFieldType Of
    0  : Result:='ftUnknown';
    1  : Result:='ftString';
    2  : Result:='ftSmallint';
    3  : Result:='ftInteger';
    4  : Result:='ftWord';
    5  : Result:='ftBoolean';
    6  : Result:='ftFloat';
    7  : Result:='ftCurrency';
    8  : Result:='ftBCD';
    9  : Result:='ftDate';
    10 : Result:='ftTime';
    11 : Result:='ftDateTime';
    12 : Result:='ftBytes';
    13 : Result:='ftVarBytes';
    14 : Result:='ftAutoInc';
    15 : Result:='ftBlob';
    16 : Result:='ftMemo';
    17 : Result:='ftGraphic';
    18 : Result:='ftFmtMemo';
    19 : Result:='ftParadoxOle';
    20 : Result:='ftDBaseOle';
    21 : Result:='ftTypedBinary';
    22 : Result:='ftCursor';
  End;
End;
Function GetValidPath ( aPath : String; bWantSlash : Boolean ) : String;
Var
  LastCh : String;
Begin
  Result:=aPath;
  LastCh:=Copy(aPath,Length(aPath),1);
  If bWantSlash Then
  Begin
    If (LastCh='\') Then
      Exit
    Else
      Result:=aPath+'\'
  End Else
  Begin
    If (LastCh<>'\') Then
      Exit
    Else
      Result:=Copy(aPath,1,Length(aPath)-1);
  End;
End;
Function GetFieldDefRec ( aName      : String;
                          aDataType  : TFieldType;
                          aSize      : Word;
                          aRequired  : Boolean): TFieldDefRec;
Begin
  With Result Do
  Begin
    Name:=aName;
    DataType:=aDataType;
    Size:=aSize;
    Required:=aRequired;
  End;
End;
Function GetIndexDefRec ( aName      ,
                          aFields    : String;
                          aOptions   : TIndexOptions):TIndexDefRec;
Begin
  With Result Do
  Begin
    Name:=aName;
    Fields:=aFields;
    Options:=aOptions;
  End;
End;
Function OpenTable (   aOwner        : TComponent;
                       sTableName    ,
                       sDataBaseName ,
                       sAutoIdName   : String;
                       aTableType    : TTableType;
                       aFieldsDefs   : Array of TFieldDefRec;
                       aIndexDefs    : Array of TIndexDefRec;
                       bCreateActive : Boolean): TTable;
Var
  C : Integer;
Begin
  Result:=TTable.Create(aOwner);
  With Result Do
  Begin
    Active := False;
    DatabaseName := sDataBaseName;
    TableType := aTableType;
    TableName := sTableName;
    If sAutoIdName='' Then sAutoIdName:='ID';
    If Not FileExists(GetValidPath ( sDataBaseName, True ) +
sTableName+'.DB') Then
    Begin
      With FieldDefs Do
        Add(sAutoIdName,ftAutoInc,0,True);
      For C:=0 To High(aFieldsDefs) Do
        With FieldDefs Do
          Add( aFieldsDefs[C].Name,
               aFieldsDefs[C].DataType,
               aFieldsDefs[C].Size,
               aFieldsDefs[C].Required);
      With IndexDefs Do
        Add('',sAutoIdName,[ixPrimary, ixUnique]);
      For C:=0 To High(aIndexDefs) Do
        With IndexDefs Do
Add(aIndexDefs[C].Name,aIndexDefs[C].Fields,aIndexDefs[C].Options);
      CreateTable;
    End;
    SessionName:=Session.SessionName;
    Active:=bCreateActive;
  End;
End;
End.

> Chi può darmi qualche info su come inizializzare un Database?
Che database vuoi usare?
Se usi paradox, allora la cosa migliore è usare il Database Desktop e
crearti la/le tue tavola/le, poi creare un alias tramite il BDE
Administrator e
quindi usare gli oggetti delphi (TTable, TQuery), impostando la loro
proprietà database all'alias che hai creato.
Se hai altre domande, postale pure,

non smetterň mai di dirlo prova i componenti ADO.
č piů facile da utilizzare, il bde č un casino e poi ti devi
portare i driver i giro.
--
Gna...@Wappi.com
ICQ# 76488613
http://gnaffy.tripod.com/

"Gna...@Wappi.com" wrote:

Allora potresti provare Interbase 6 beta (che č gratuito) e ti devi
portare dietro solo 1 dll, per accedervi come client.
--
Massimo
l.sky...@iname.com
--------------------------------------
Lasciate ogni speranza, voi ch'entrate
--------------------------------------

 portare dietro solo 1 dll, per accedervi come client.
Non č piů beta, č uscita la versione definitiva.
-- 
Roberto Lupi

Roberto Lupi wrote:

Grazie
