如何在程序中动态创建paradox表 定义字段,索引(50分)

B

bird

Unregistered / Unconfirmed
GUEST, unregistred user!
如题
我的目的是创建临时库 如有其他方法请赐教
具体代码请详细些
谢谢
 
用SQL的create table可以直接实现建表,
alter table即可修改。
 
太简单了吧
 
以下是Delphi 3中 TTable中Create Table的例子, 我作了一些说明:

with Table1 do

begin
Active := False; { The Table component must not be active }

{ First, describe the type of table and give it a name }
DatabaseName := 'DBDEMOS';
TableType := ttParadox;
TableName := 'CustInfo';
//若不想指定某数据库, 而在本地硬盘上建立临时数据时,用以下方式替代以上三行
// DatabaseName := '';
// TableType := ttParadox;
// TableName := 'C:/Temp/CustInfo.db';

{ Next, describe the fields in the table }
with FieldDefs do
begin
Clear;
Add('Field1', ftInteger, 0, True);
Add('Field2', ftString, 30, False);
end;

{ Next, describe any indexes }
with IndexDefs do

begin
Clear;
{ The first index has no name because it is a Paradox primary key }
Add('', 'Field1', [ixPrimary, ixUnique]);
Add('Fld2Indx', 'Field2', [ixCaseInsensitive]);
end;

{ Now that we have specified what we want, create the table }
CreateTable;
end;

----------------------------------------------------------------------
若使用想SQL语句建立; 可以用下列方式(我经常使用SQL);

const CreateSQL='Create Table "%s" (%s)'; //SQL建表框架
var TempQuery:Tquery;
DefField :string; //字段名和数据类型描述部分
tableName :string; //表名称
begin
//字段名和数据类型描述, 每个字段用,号分开
Deffield := ' Field1 Integer, Field2 CHAR(30) ';

tableName := 'C:/TEMP/DB01.DB';
TempQuery := Tquery.Create(application); //建立TQuery对象
with TempQuery do begin
try
SQL.TEXT := format(CreateSQL,[tableName,deffield]); //填充Tquery中的SQL语句;
try
ExecSQL; //执行SQL命令
except
//..错误处理
end;
finally
free; //释放Tquery对象
end;
end;

end;

具体SQL的语法参看Delphi中Local SQL Help ;
那有create Table, create Index, drop table等语句的语法介绍
 
如果要照已有的原库建数据库,可以这样

tblOld.Open; tblOld.Close;
....
tblNew.FieldDefs.Assign(tblOld.FieldDefs);
tblNew.IndexDefs.Assign(tblOld.IndexDefs);
tblNew.Create;

 
感谢 SeaSky
还有一个小问题
如何在Post之前得到自增字段的值
 
如果谁能解释一下如何用程序删库,清库.
这样这个问题就更完美了!!
 
TTable有DeleteTable和EmptyTable两个方法.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部