如何给一个已存在的表新加一个字段?

fee 2000-01-03 03:00:00
如何给一个已存在的表新加一个字段?
...全文
312 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
spear 2000-01-05
  • 打赏
  • 举报
回复
zdg方法正确
qingqing 2000-01-05
  • 打赏
  • 举报
回复
用我的MyTable构件完全可以解决这个问题,同时能保证不丢失原有的记录。若需要,请发电子邮件给我,告诉我你的电子邮件地址,我把MyTable构件发给你,并告诉你使用方法,OK?
tiger 2000-01-04
  • 打赏
  • 举报
回复
这个问题一点也不复杂, 用zdg的方法, 再加上另一句sql
Create index index_name on table(column_name...)
Then OK.
zdg 2000-01-04
  • 打赏
  • 举报
回复
SQL语句的
Alter Table table_name add column_name data_type
可以解决你的问题...
barton 2000-01-04
  • 打赏
  • 举报
回复
初学者何必在意这么复杂的问题.没法简单.
不过你可以重新建一个表:
1.定义FieldDefs
2.定义IndexDefs
3.用TTable.CreateTable方法建立新表
4.用BatchMove将原表格中的记录复制过来
5.删除旧表
fee 2000-01-03
  • 打赏
  • 举报
回复
menxin:
能否简单点?毕竟我是初学者。
fee 2000-01-03
  • 打赏
  • 举报
回复
smartkid:
如果要定义索引名以及字段名又应怎么办?
smartkid 2000-01-03
  • 打赏
  • 举报
回复
AQuery.SQL.Text := 'ALTER TABLE MyTable ADD NewColumn INTEGER'; //不同数据库语句略有差别
AQuery.ExecSQL;
menxin 2000-01-03
  • 打赏
  • 举报
回复
你的意思是用代码吧。 :-)
总的来说是用dbidorestruct bde api,d5的例子中直接用一个例子,自己改改吧。
总之,databasedesktop中restruck的功能都可用它实现,如加口令等。

Example 3: Alter a field in a Paradox or dBASE table.

This example will alter an existing field in a Paradox or dBASE table. NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option is not used in the restructure. FillChar can be used to do this:

Fillchar(MyChangeRec, sizeof(MyChangeRec), 0);

This example uses the following input:

ChangeField(Table1, Table1.FieldByName('FOO'), MyChangeRec)

ChangeRec is defined as follows:

type

ChangeRec = packed record
szName: DBINAME;
iType: Word;
iSubType: Word;
iLength: Word;
iPrecision: Byte;
end;


The function is defined as follows:

procedure ChangeField(Table: TTable; Field: TField; Rec: ChangeRec);

var
Props: CURProps;
hDb: hDBIDb;
TableDesc: CRTblDesc;
pFields: pFLDDesc;
pOp: pCROpType;
B: Byte;
begin
// Initialize the pointers...
pFields := nil;
pOp := nil;
// Make sure the table is open exclusively so we can get the db handle...
if not Table.Active then
raise EDatabaseError.Create('Table must be opened to restructure');
if not Table.Exclusive then
raise EDatabaseError.Create('Table must be opened exclusively' +

'to restructure');
Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, Integer(xltNONE)));
// Get the table properties to determine table type...
Check(DbiGetCursorProps(Table.Handle, Props));
// Make sure the table is either Paradox or dBASE...
if (Props.szTableType <> szPARADOX) and (Props.szTableType <> szDBASE) then
raise EDatabaseError.Create('Field altering can only occur on Paradox' +
' or dBASE tables');

// Allocate memory for the field descriptor...
pFields := AllocMem(Table.FieldCount * sizeof(FLDDesc));
// Allocate memory for the operation descriptor...
pOp := AllocMem(Table.FieldCount * sizeof(CROpType));
try
// Set the pointer to the index in the operation descriptor to put
// crMODIFY (This means a modification to the record is going to happen)...
Inc(pOp, Field.Index);
pOp^ := crMODIFY;
Dec(pOp, Field.Index);
// Fill the field descriptor with the existing field information...

Check(DbiGetFieldDescs(Table.Handle, pFields));
// Set the pointer to the index in the field descriptor to make the
// midifications to the field
Inc(pFields, Field.Index);
// If the szName portion of the ChangeRec has something in it, change it...
if (Length(Rec.szName) > 0) then
pFields^.szName := Rec.szName;
// If the iType portion of the ChangeRec has something in it, change it...
if (Rec.iType > 0) then

pFields^.iFldType := Rec.iType;
// If the iSubType portion of the ChangeRec has something in it, change it...
if (Rec.iSubType > 0) then
pFields^.iSubType := Rec.iSubType;
// If the iLength portion of the ChangeRec has something in it, change it...
if (Rec.iLength > 0) then
pFields^.iUnits1 := Rec.iLength;
// If the iPrecision portion of the ChangeRec has something
// in it, change it...
if (Rec.iPrecision > 0) then

pFields^.iUnits2 := Rec.iPrecision;
Dec(pFields, Field.Index);
for B := 1 to Table.FieldCount do begin
pFields^.iFldNum := B;
Inc(pFields, 1);
end;
Dec(pFields, Table.FieldCount);

// Blank out the structure...
FillChar(TableDesc, sizeof(TableDesc), #0);
// Get the database handle from the table's cursor handle...
Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));

// Put the table name in the table descriptor...
StrPCopy(TableDesc.szTblName, Table.TableName);
// Put the table type in the table descriptor...
StrPCopy(TableDesc.szTblType, Props.szTableType);
// The following three lines are necessary when doing any field restructure
// operations on a table...

// Set the field count for the table
TableDesc.iFldCount := Table.FieldCount;
// Link the operation descriptor to the table descriptor...

TableDesc.pecrFldOp := pOp;
// Link the field descriptor to the table descriptor...
TableDesc.pFldDesc := pFields;
// Close the table so the restructure can complete...
Table.Close;
// Call DbiDoRestructure...
Check(DbiDoRestructure(hDb, 1, @TableDesc,
nil, nil, nil, False));
finally
if (pFields <> nil) then
FreeMem(pFields);
if (pOp <> nil) then

FreeMem(pOp);
end;
end;

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧