Einige Quellcode Beispiele auf Basis Borland Delphi und TurboDB VCL 4.3.
|
| Interface Definition |
Um die Beispiele verwenden zu können, muss im Interface Teil definiert sein ...
Interface
Uses
... Db, TdbDataSet, TdbQuery, TdbBatchMove;
Type
TDBModul = class(TDataModule)
DBDatabase : TTdbDatabase;
DBTable : TTdbTable;
DBQuery : TTdbQuery;
DBDataSource: TDataSource;
...
| | TDBGetDataType |
Function TDBGetDataType(T : TTdbDataType) : String;
Begin
Case T of
dtAutoInc : Result := 'AutoInc';
dtBlob : Result := 'Blob';
dtBoolean : Result := 'Boolean';
dtByte: Result := 'Byte';
dtDate : Result := 'Date';
dtDateTime : Result := 'DateTime';
dtEnum : Result := 'Enum';
dtFloat : Result := 'Float';
dtGUID : Result := 'GUID';
dtInteger : Result := 'Integer';
dtLink : Result := 'Link';
dtMemo : Result := 'Memo';
dtRelation : Result := 'Relation';
dtSmallInt : Result := 'SmallInt';
dtString : Result := 'String';
dtTime : Result := 'Time';
dtUnknown : Result := 'Unknown';
dtWideMemo : Result := 'WideMemo';
dtWideString : Result := 'WideString'
Else
Result := 'Unknown';
End;
End;
| | TDBSQLExecute |
Function TDBSQLExecute(theQuery : TTdbQuery; sSQLCmd : String) : String;
Begin
Result := '';
If sSQLCmd = '' Then
Exit;
theQuery.Close;
theQuery.SQL.Clear;
theQuery.SQL.Add(sSQLCmd);
Try
If LowerCase(Copy(TrimLeft(sSQLCmd),1,6)) = 'select' Then
theQuery.Open
Else
theQuery.ExecSQL;
Except
On e: EAccessViolation Do Begin
Result := Format('%s',[e.Message]);
Exit;
End;
On e: ETurboDBError Do Begin
Result := Format('Error executing SQL command: ' + #13#10 + '%s %d', [e.Message, e.TdbError]);
Exit;
End;
On e: EDbEngineError do Begin
Result := Format('%s',[e.Message]);
Exit;
End;
On e: EDatabaseError do Begin
Result := Format('%s',[e.Message]);
Exit;
End;
End;
End;
| | TDBGetFieldNr |
Function TDBGetFieldNr(DS : TDataSet;sFieldName : String) : Integer;
Var i : Integer;
Begin
Result := -1;
If DS.FieldCount = 0 Then Begin
Result := -2;
Exit;
End;
For i:= 0 To DS.FieldCount - 1 Do Begin
If DS.Fields[i].Name = sFieldName Then
Result := i;
End;
End;
| | TDBGetFieldName |
Function TDBGetFieldName(DS : TDataSet;nFieldNr : Integer) : String;
Var i : Integer;
begin
Result := '';
If DS.FieldCount = 0 Then Exit;
For i:= 0 To DS.FieldCount - 1 Do Begin
If i = nFieldNr Then
Result := DS.Fields[i].Name;
End;
end;
| | TDBGetFieldSize |
Function TDBGetFieldSize(DS : TDataSet;nFieldNr : Integer) : Integer;
Var i : Integer;
begin
Result := -1;
If DS.FieldCount = 0 Then Exit;
For i:= 0 To DS.FieldCount - 1 Do Begin
If i = nFieldNr Then
Result := DS.Fields[i].Size;
End;
end;
| | TDBGetFieldSize2 |
Function TDBGetFieldSize2(DS : TDataSet;sFieldName : String) : Integer;
Var i, nFieldNr : Integer;
begin
Result := -1;
If DS.FieldCount = 0 Then Exit;
nFieldNr := TDBGetFieldNr(DS, sFieldName);
If nFieldNr = -1 Then Exit;
For i:= 0 To DS.FieldCount - 1 Do
Begin
If i = nFieldNr Then
Result := DS.Fields[i].Size;
End;
end;
| | TDBFileIsTable |
Function TDBFileIsTable(sFile : String) : Boolean;
Begin
sFile := Lowercase(sFile);
Result := False;
If (URightStr(sFile, 3) = 'dat') Or (URightStr(sFile, 3) = 'rel') Then
Result := True
End;
| | TDBGetFields |
Function TDBGetFields(sDBFile : String) : TStringList;
//databasetype = dbdirectory
Var fSL : TStringList;
sDBName, sDBPath : String;
Begin
fSL := TStringList.Create;
Result := fSL;
sDBPath := ExtractFilePath(sDBFile);
sDBName := ExtractFileName(sDBFile);
DBModul.DBDatabase.Location := sDBPath;
DBModul.DBDataBase.Connected := True;
DBModul.DBDataBase.ReadOnly := True;
DBModul.DBTable.TableName := sDBName;
DBModul.DBTable.ReadOnly := True;
Try
DBModul.DBTable.GetFieldNames(fSL);
Except
On e: EAccessViolation Do Begin
MessageDlg(Format('%s',[e.Message]),mtError, [mbOK], 0);
fSL.Add(Format('%s',[e.Message]));
Exit;
End;
On e: ETurboDBError Do Begin
fSL.Add(Format('%s Code = %d',[e.Message, e.TdbError]));
Exit;
End;
End;
DBModul.DBDataBase.Connected := False;
Result := fSL;
End;
//some alternative code using a var called sDBType which is either directory or singlefile
//the fields are stored in a listbox called HTMllbTables
If (LowerCase(sDBType) = 'directory') Then Begin
DBModul.DBDatabase.Connected := False;
DBModul.DBDatabase.Location := Lowercase(sDBPath);
DBModul.DBDatabase.DataBaseType := dbtDirectory;
DBModul.DBDatabase.Connected := True;
//get all the "dat" files from the directory; possible to look after "rel" as well
HTMLlbTables.Items := UDirScanPath(sDBPath, '*.dat', False);
End;
If (LowerCase(sDBType) = 'singlefile') Then Begin
DBModul.DBDatabase.Connected := False;
DBModul.DBDatabase.Location := Lowercase(sDBPath);
DBModul.DBDatabase.DataBaseType := dbtSingleFile;
DBModul.DBDatabase.Connected := True;
HTMLlbTables.Items := DBModul.DBDatabase.CreateTableNameList;
End;
//just convert all the fieldnames to lowercase
If HTMLlbTables.Items.Count > 0 Then For i := 0 To HTMLlbTables.Items.Count - 1 Do
HTMLlbTables.Items[i] := LowerCase(HTMLlbTables.Items[i]);
| | TDBGetIndices |
Function TDBGetIndices(sDBFile : String) : TStringList;
Var fSL : TStringList;
nMaxIndex, j : Integer;
sDBName, sDBPath : String;
begin
fSL := TStringList.Create;
Result := fSL;
sDBPath := ExtractFilePath(sDBFile);
sDBName := ExtractFileName(sDBFile);
DBModul.DBDatabase.Directory := sDBPath;
DBModul.DBDataBase.Connected := True;
DBModul.DBTable.TableName := sDBName;
Try
DBModul.DBTable.Active := True;
nMaxIndex := DBModul.DBTable.IndexDefs.Count;
For j := 0 To nMaxIndex - 1 Do
With DBModul.DBTable.IndexDefs.Items[j] Do
fSL.Add(Fields);
Except
On e: EAccessViolation Do Begin
MessageDlg(Format('%s',[e.Message]),mtError, [mbOK], 0);
fSL.Add(Format('%s',[e.Message]));
Exit;
End;
On e: ETurboDBError Do Begin
fSL.Add(Format('%s Code = %d',[e.Message, e.TdbError]));
Exit;
End;
End;
DBModul.DBTable.Active := False;
DBModul.DBDataBase.Connected := False;
Result := fSL;
End;
| | TDBSetDataType |
Function TDBSetDataType(sType : String) : TTdbDataType;
Begin
TDBSetDataType := dtUnknown ;
If sType = 'AutoInc' Then TDBSetDataType := dtAutoInc ;
If sType = 'Blob' Then TDBSetDataType := dtBlob ;
If sType = 'Boolean' Then TDBSetDataType := dtBoolean ;
If sType = 'Byte' Then TDBSetDataType := dtByte;
If sType = 'Date' Then TDBSetDataType := dtDate ;
If sType = 'DateTime' Then TDBSetDataType := dtDateTime ;
If sType = 'Enum' Then TDBSetDataType := dtEnum ;
If sType = 'Float' Then TDBSetDataType := dtFloat ;
If sType = 'GUID' Then TDBSetDataType := dtGUID ;
If sType = 'Integer' Then TDBSetDataType := dtInteger ;
If sType = 'LargeInt' Then TDBSetDataType := dtLargeInt ;
If sType = 'Link' Then TDBSetDataType := dtLink ;
If sType = 'Memo' Then TDBSetDataType := dtMemo ;
If sType = 'Relation' Then TDBSetDataType := dtRelation ;
If sType = 'SmallInt' Then TDBSetDataType := dtSmallInt ;
If sType = 'String' Then TDBSetDataType := dtString ;
If sType = 'Time' Then TDBSetDataType := dtTime ;
If sType = 'Unknown' Then TDBSetDataType := dtUnknown ;
If sType = 'WideMemo' Then TDBSetDataType := dtWideMemo ;
If sType = 'WideString' Then TDBSetDataType := dtWideString ;
End;
| | TDBSQLSetDataType |
Function TDBSQLSetDataType(sType : String) : String;
Begin
sType := UpperCase(sType);
Result := sType;
If sType ='AUTOINC' Then Result :='AUTOINC';
If sType ='STRING' Then Result :='CHAR';
If sType ='WIDESTRING' Then Result :='WCHAR';
If sType ='BYTE' Then Result :='BYTE';
If sType ='SMALLINT' Then Result :='SMALLINT';
If sType ='INTEGER' Then Result :='INTEGER';
If sType ='BIGINT' Then Result :='BIGINT';
If sType ='FLOAT' Then Result :='DOUBLE PRECISION';
If sType ='BOOLEAN' Then Result :='BOOLEAN';
If sType ='TIME' Then Result :=' TIME';
If sType ='DATE' Then Result :='DATE';
If sType ='DATETIME' Then Result :='TIMESTAMP';
If sType ='ENUM' Then Result :='ENUM';
//If sType ='STRING' Then Result :='VARCHAR';
//If sType ='WIDESTRING' Then Result :='VARWCHAR';
If sType ='MEMO' Then Result :='LONGVARCHAR';
If sType ='WIDEMEMO' Then Result :='LONGVARWCHAR';
If sType ='BLOB' Then Result :='LONGVARBINARY';
If sType ='LINK' Then Result :='LINK';
If sType ='RELATION' Then Result :='RELATION';
If sType ='GUID' Then Result :='GUID';
End;
| | TDBGetIndexOption |
Function TDBGetIndexOption(T : TIndexOptions) : String;
Var sStr : String;
Begin
sStr := '';
If ixPrimary In T Then sStr := sStr + 'Primary';
If ixUnique In T Then sStr := sStr + 'Unique';
If ixExpression In T Then sStr := sStr + 'Expression';
If ixNonMaintained In T Then sStr := sStr + 'NonMaintained';
If ixDescending In T Then sStr := sStr + 'Descending';
If ixCaseInsensitive In T Then sStr := sStr + 'CaseInsensitive';
Result := sStr;
End;
| | TDBGetAutoField |
Function TDBGetAutoField(DS : TDataset) : Integer;
Var i : Integer;
Begin
Result := -1;
If DS.FieldCount > 0 Then
For i:= 0 To DS.FieldCount - 1 Do
If DS.Fields[i].Datatype = ftAutoInc Then
Result := i;
End;
| | TDBGetTableInfo |
Function TDBGetTableInfo(sDBPfad : String) : String;
Var sDBPath, sDBName, sInfo : String;
Begin
sInfo := '';
If Not FileExists(sDBPfad) Then
Begin
Result := sInfo;
Exit;
End;
sDBPath := ExtractFilePath(sDBPfad);
sDBName := ExtractFileName(sDBPfad);
DBModul.DBDatabase.Directory := sDBPath;
DBModul.DBDataBase.Connected := True;
DBModul.DBDataSource.DataSet := DBModul.DBQuery;
DBModul.DBTable.TableName := sDBName;
DBModul.DBTable.Active := True;
sInfo := sInfo + 'DatabaseName='+DBModul.DBDatabase.Name+#13#10;
sInfo := sInfo + 'DatabaseDirectory='+DBModul.DBDatabase.Directory+#13#10;
sInfo := sInfo + 'TableFileName='+DBModul.DBTable.TableFileName+#13#10;
sInfo := sInfo + 'TableName='+DBModul.DBTable.TableName+#13#10;
sInfo := sInfo + 'TableLevel='+UInt2Str(DBModul.DBTable.TableLevel)+#13#10;
sInfo := sInfo + 'Key='+UInt2Str(DBModul.DBTable.Key)+#13#10;
sInfo := sInfo + 'LangDriver='+DBModul.DBTable.LangDriver+#13#10;
sInfo := sInfo + 'Password='+DBModul.DBTable.Password+#13#10;
sInfo := sInfo + 'DetailFields='+DBModul.DBTable.DetailFields+#13#10;
DBModul.DBTable.Active := False;
DBModul.DBDataBase.Connected := False;
Result := sInfo;
End;
| | GetTableProperties |
Procedure GetTableProperties(Sender: TObject; sTable, sDBPath : String);
//get all the table properties and store these in an inspectorbar.
//can use stringlist , listbox ... instead of inspectorbar
Var sInfo : String;
i : Integer;
begin
If sTable = '' Then Exit;
DBModul.DBTable.TableName := sTable;
Try
With DBModul.DBTable Do Begin
Active := True;
InspectorBar1.Panels[0].Items[0].Textvalue := sTable;
InspectorBar1.Panels[0].Items[1].Textvalue := sDBPath;
InspectorBar1.Panels[0].Items[2].Textvalue := UInt2Str(TableLevel);
InspectorBar1.Panels[0].Items[3].Textvalue := UInt2Str(RecordCount);
InspectorBar1.Panels[0].Items[4].Textvalue := UGetFileInfo(sDBPath + sDBFile);
//fields
InspectorBar1.Panels[0].Items[5].Textvalue := UInt2Str(FieldCount);
InspectorBar1.Panels[0].Items[6].Values.Clear;
For i:=0 To FieldCount - 1 Do With InspectorBar1.Panels[0].Items[6].Values Do Begin
sInfo := Fields[i].FieldName;
sInfo := sInfo + ' (';
sInfo := sInfo + TDBGetDataType(FieldDefsTdb.Items[i].DataTypeTdb) + ' ';
If FieldDefsTdb.Items[i].Size > 0 Then
sInfo := sInfo + UInt2Str(FieldDefsTdb.Items[i].Size) + ' ';
If FieldDefsTdb.Items[i].Precision > 0 Then
sInfo := sInfo + UInt2Str(FieldDefsTdb.Items[i].Precision) + ' ';
If FieldDefsTdb.Items[i].Required = True Then
sInfo := sInfo + 'Required' + ' ';
If FieldDefsTdb.Items[i].Expression <> '' Then
sInfo := sInfo + FieldDefsTdb.Items[i].Expression + ' ';
If FieldDefsTdb.Items[i].Specification <> '' Then
sInfo := sInfo + FieldDefsTdb.Items[i].Specification + ' ';
sInfo := sInfo + ')';
Add(sInfo);
End;
If FieldCount > 0 Then
InspectorBar1.Panels[0].Items[6].TextValue := InspectorBar1.Panels[0].Items[6].Values[0];
//indices
InspectorBar1.Panels[0].Items[7].Textvalue := UInt2Str(IndexDefs.Count);
InspectorBar1.Panels[0].Items[8].Values.Clear;
For i:=0 To IndexDefs.Count - 1 Do With InspectorBar1.Panels[0].Items[8].Values Do Begin
sInfo := IndexDefs.Items[i].Name + ' (' + IndexDefs.Items[i].Fields + ')';
Add(sInfo);
End;
If IndexDefs.Count > 0 Then
InspectorBar1.Panels[0].Items[8].TextValue := InspectorBar1.Panels[0].Items[8].Values[0];
//
Active := False;
End;
Except
End;
End;
|