1、通过DataSet组件执行SQL语句创建 2、通过BDE引擎创建执行不起来,需要专门的配置文件配合,可以通过BDE建任何通过BDE连接的数据库表,自己参考一下。//根据字段列表创建数据表 //dbtype 数据库类型 如果是ORACLE 则MEMO型该为STRING型 Function CreateDbTable(DataBaseName,SessionName,DbType : String;TableName : String; AFieldList,AIndexList : TStringList;AFlag : Boolean) : Integer; var TmpTb : TTable; FieldLen,J : Integer; TmpS,TmpS1 : String; FieldName : String; FieldDataType : TFieldType; FieldRequired : Boolean; sIndexName,sIndexFields,sIndexOption : String; CanContinue : Boolean; IndexFieldList : TStringList; Begin TmpTb := TTable.Create(nil); IndexFieldList := TStringList.Create; TmpTb.DatabaseName := DataBaseName; If SessionName <> '' Then TmpTb.SessionName := SessionName; TmpTb.TableName := UpperCase(TableName); CanContinue := True; If Not AFlag Then Begin If TmpTb.Exists Then CanContinue := False; End; If CanContinue Then Begin With TmpTb Do Begin TableType := ttParadox; Close; Try FieldDefs.Clear; For J := 0 To AFieldList.Count - 1 Do Begin FieldName := UpperCase(AFieldList.Names[J]); TmpS := AFieldList.Values[AFieldList.Names[J]]; TmpS1 := Trim(GetSubStr(TmpS,',',1)); If IsInteger(TmpS1) Then FieldLen := StrToInt(TmpS1) Else FieldLen := 0; TmpS1 := Trim(UpperCase(GetSubStr(TmpS,',',0))); If TmpS1 = 'S' Then Begin If (FieldLen > 255) And (Pos('ORACLE',UpperCase(DbType)) <=0) Then FieldDataType := ftMemo Else FieldDataType := ftString End Else If TmpS1 = 'V' Then Begin If (FieldLen > 255) And (Pos('ORACLE',UpperCase(DbType)) <=0) Then FieldDataType := ftMemo Else FieldDataType := ftString; End Else If TmpS1 = 'I' Then Begin FieldDataType := ftInteger; FieldLen := 0; End Else If TmpS1 = 'D' Then Begin FieldDataType := ftDateTime; FieldLen := 0; End Else If TmpS1 = 'F' Then Begin FieldDataType := ftFloat; FieldLen := 0; End Else If TmpS1 = 'L' Then Begin FieldDataType := ftBlob; FieldLen := 0; End Else If TmpS1 = 'B' Then Begin FieldDataType := ftBoolean; FieldLen := 1; End Else FieldDataType := ftString; TmpS1 := UpperCase(Trim(GetSubStr(TmpS,',',2))); If TmpS1 = 'N' Then FieldRequired := True Else FieldRequired := False; FieldDefs.Add(FieldName,FieldDataType,FieldLen,FieldRequired); End; IndexDefs.Clear; For J := 0 To AIndexList.Count - 1 Do Begin sIndexName := AIndexList.Names[J]; sIndexFields := AIndexList.Values[AIndexList.Names[J]]; If Pos(',',sIndexFields) > 0 Then Begin sIndexOption := Copy(sIndexFields,Pos(',',sIndexFields)+1, 1); sIndexFields := Copy(sIndexFields,1,Pos(',',sIndexFields)-1); End Else sIndexOption := ''; If IndexFieldList.IndexOf(sIndexFields) >=0 Then Continue Else IndexFieldList.Add(sIndexFields); If Uppercase(sIndexOption) = 'P' Then Begin If DbType = 'STANDARD' Then IndexDefs.Add('',sIndexFields,[ixPrimary,ixUnique]) //只创建主键 else IndexDefs.Add(sIndexName,sIndexFields,[ixPrimary,ixUnique]); End Else If Uppercase(sIndexOption) = 'U' Then Begin IndexDefs.Add(sIndexname,sIndexFields,[ixUnique]) End Else Begin If DbType = 'STANDARD' Then IndexDefs.Add(sIndexName,sIndexFields,[ixCaseInsensitive]) Else IndexDefs.Add(sIndexName,sIndexFields,[]); end; End; CreateTable; Result := 0; Except On E : Exception Do begin WriteLogFile(ExtractFilePath(Application.ExeName) + 'createtable.log', '创建表'+TableName + '失败 ' + E.Message + ' ' + FormatDateTime('MM"月"DD"日" HH:MM:SS',Now) ); zlMessageDlg('创建数据表'+TableName + '失败! ' + E.Message,mtWarning,[mbOk],0); Result := -1; end; End End; End Else Result := 1; IndexFieldList.Free; TmpTb.Free; End;
2、通过BDE引擎创建执行不起来,需要专门的配置文件配合,可以通过BDE建任何通过BDE连接的数据库表,自己参考一下。//根据字段列表创建数据表
//dbtype 数据库类型 如果是ORACLE 则MEMO型该为STRING型
Function CreateDbTable(DataBaseName,SessionName,DbType : String;TableName : String;
AFieldList,AIndexList : TStringList;AFlag : Boolean) : Integer;
var
TmpTb : TTable;
FieldLen,J : Integer;
TmpS,TmpS1 : String;
FieldName : String;
FieldDataType : TFieldType;
FieldRequired : Boolean;
sIndexName,sIndexFields,sIndexOption : String;
CanContinue : Boolean;
IndexFieldList : TStringList;
Begin
TmpTb := TTable.Create(nil);
IndexFieldList := TStringList.Create;
TmpTb.DatabaseName := DataBaseName;
If SessionName <> '' Then
TmpTb.SessionName := SessionName;
TmpTb.TableName := UpperCase(TableName);
CanContinue := True;
If Not AFlag Then
Begin
If TmpTb.Exists Then
CanContinue := False;
End;
If CanContinue Then
Begin
With TmpTb Do
Begin
TableType := ttParadox;
Close;
Try
FieldDefs.Clear;
For J := 0 To AFieldList.Count - 1 Do
Begin
FieldName := UpperCase(AFieldList.Names[J]);
TmpS := AFieldList.Values[AFieldList.Names[J]];
TmpS1 := Trim(GetSubStr(TmpS,',',1));
If IsInteger(TmpS1) Then
FieldLen := StrToInt(TmpS1)
Else
FieldLen := 0;
TmpS1 := Trim(UpperCase(GetSubStr(TmpS,',',0)));
If TmpS1 = 'S' Then
Begin
If (FieldLen > 255) And (Pos('ORACLE',UpperCase(DbType)) <=0) Then
FieldDataType := ftMemo
Else
FieldDataType := ftString
End
Else If TmpS1 = 'V' Then
Begin
If (FieldLen > 255) And (Pos('ORACLE',UpperCase(DbType)) <=0) Then
FieldDataType := ftMemo
Else
FieldDataType := ftString;
End
Else If TmpS1 = 'I' Then
Begin
FieldDataType := ftInteger;
FieldLen := 0;
End
Else If TmpS1 = 'D' Then
Begin
FieldDataType := ftDateTime;
FieldLen := 0;
End
Else If TmpS1 = 'F' Then
Begin
FieldDataType := ftFloat;
FieldLen := 0;
End
Else If TmpS1 = 'L' Then
Begin
FieldDataType := ftBlob;
FieldLen := 0;
End
Else If TmpS1 = 'B' Then
Begin
FieldDataType := ftBoolean;
FieldLen := 1;
End
Else
FieldDataType := ftString;
TmpS1 := UpperCase(Trim(GetSubStr(TmpS,',',2)));
If TmpS1 = 'N' Then
FieldRequired := True
Else
FieldRequired := False;
FieldDefs.Add(FieldName,FieldDataType,FieldLen,FieldRequired);
End;
IndexDefs.Clear;
For J := 0 To AIndexList.Count - 1 Do
Begin
sIndexName := AIndexList.Names[J];
sIndexFields := AIndexList.Values[AIndexList.Names[J]];
If Pos(',',sIndexFields) > 0 Then
Begin
sIndexOption := Copy(sIndexFields,Pos(',',sIndexFields)+1,
1);
sIndexFields := Copy(sIndexFields,1,Pos(',',sIndexFields)-1);
End
Else
sIndexOption := '';
If IndexFieldList.IndexOf(sIndexFields) >=0 Then
Continue
Else
IndexFieldList.Add(sIndexFields);
If Uppercase(sIndexOption) = 'P' Then
Begin
If DbType = 'STANDARD' Then
IndexDefs.Add('',sIndexFields,[ixPrimary,ixUnique]) //只创建主键
else
IndexDefs.Add(sIndexName,sIndexFields,[ixPrimary,ixUnique]);
End
Else If Uppercase(sIndexOption) = 'U' Then
Begin
IndexDefs.Add(sIndexname,sIndexFields,[ixUnique])
End
Else
Begin
If DbType = 'STANDARD' Then
IndexDefs.Add(sIndexName,sIndexFields,[ixCaseInsensitive])
Else
IndexDefs.Add(sIndexName,sIndexFields,[]);
end;
End;
CreateTable; Result := 0;
Except On E : Exception Do
begin
WriteLogFile(ExtractFilePath(Application.ExeName) + 'createtable.log',
'创建表'+TableName + '失败 ' + E.Message + ' ' +
FormatDateTime('MM"月"DD"日" HH:MM:SS',Now) );
zlMessageDlg('创建数据表'+TableName +
'失败! ' + E.Message,mtWarning,[mbOk],0); Result := -1;
end;
End
End;
End
Else
Result := 1;
IndexFieldList.Free;
TmpTb.Free;
End;