在Form1.FormCreate事件中写入如下代码,程序启动时,将自动检测是否存在数据库别名Cntssamp,如果没有则建立之;自动检测别名Cntssamp中是否存在表格TSK(图书库),如果没有则自动建立表格TSK。
  procedure TForm1.FormCreate(Sender: TObject);
  var
    ap:TStringList; {字符串列表变量}
      answer:Integer;
  begin
     ap:=TStringlist.Create;
     Session.GetAliasNames(ap); {取得别名列表}
     if (ap.IndexOf('Cntssamp')=-1) then {判断别名是否存在}
     begin
       answer:=Application.MessageBox('别名Cntssamp不存在,现在创建吗?','BDE信息窗口',mb—OKCancel);{增加一个名为Cngzsamp的数据库别名}
       if answer=IDCANCEL then 
         begin
          ap.Free;
          Exit;
        end;       Session.AddStandardAlias('Cntssamp','c:\delphp11','Paradox');
       Session.SaveConfigFile; {BDE配置文件存盘}
      end ;     ap.Clear; {取得别名Cngzsamp中的所有表格名称列表}
     Session.GetTableNames('Cntssamp','',False,False,ap);
     if (ap.IndexOf('TSK')=-1) then {判断表格是否存在}
     begin
       answer:=Application.MessageBox('别名Cntssamp中不存在表格TSK,现在创建吗?','表格信息窗口',mb—OKCancel);
       if answer=IDCANCEL then 
         begin
         ap.Free;
         Exit;
       end;       with table1 do 
         begin
         Active:=false;
         DatabaseName:='Cntssamp'; {数据库别名}
         TableName:='TSK';   {表格名}
         TableType:=ttParadox; {数据库类型}
         with FieldDefs do 
           begin {增加字段}
            Clear;
            Add('SH',ftString,30,False); {书号 String(30)}
            Add('SM',ftString,30,False); {书名 String(30)}
            Add('CBS',ftString,20,False); {出版社 String(20)}
            Add('CBRQ',ftDate,0,False); {出版日期 Date}
            Add('YS',ftInteger,0,False); {页数 Integer}
         end;         with IndexDefs do 
           begin {增加索引}
            Clear; {按书号字段建立主索引}
            Add('SHSY','SH',[Primary,ixUnique]);
         end;         CreateTable; {创建表格}
       end;
     end ;     ap.free; {释放变量ap}
  end;

解决方案 »

  1.   

    直接调用sql来执行最好。如:
    /* ============================================================ */
    /*   Database name:  Demo                                       */
    /*   DBMS name:      Microsoft SQL Server 2000                  */
    /*   Created on:     2001-04-22  15:33                          */
    /* ============================================================ */if exists (select 1
                from  sysobjects
               where  id = object_id('temp')
                and   type = 'U')
       drop table temp
    go/* ============================================================ */
    /*   Table: temp                                                */
    /* ============================================================ */
    create table temp
    (
        a  integer               not null,
        b  varchar(10)           not null,
        d  datetime              null    ,
        constraint PK_TEMP primary key (a)
    )
    go
      

  2.   

    用TQuery直接调用SQL语句来建表.