Solved

Adding Table-field?

Posted on 1997-10-14
11
187 Views
Last Modified: 2010-05-18
Is it possible to add a field to a table ?
I tried to add field 'test'  like this:
Table1.Active:=False;
With Table1.FieldDefs do
Begin
Add('test',ftInteger,0,False);
End;
CreateTable;
Table1.Active:=True;

This works, but the information already in the table disappears (of course). Can I add a field without emptying the table?

Ps. I use Delphi Developer 2.0 and the table is a DBase IV.

Thank you
Erik N
0
Comment
Question by:Erik N
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347498
It's normal that you get an empty table. CreateTable is your problem. I'll look for a solution:

The CreateTable method creates a new empty database table. Before calling this method, the DatabaseName, TableName, TableType, FieldDefs and IndexDefs properties must be assigned values.
0
 

Expert Comment

by:Doldrums
ID: 1347499
The easiest way to do this would be to use an SQL statement.  Here is a code snippet for you: (must use DB, DBTables)============================
  with TQuery.Create(Application) do
    try
      SQL.Add('ALTER TABLE "<your tables path full path.dbf>"
              ADD <your field name> <data type');
      ExecSQL;
    finally
      free;
    end;
========================
See CREATE TABLE in your Borland\Common Files\BDE\LocalSQL.hlp for the allowed data_type's.

An example for the add statement would be:
  SQL.Add('ALTER TABLE "C:\test.dbf" ADD NAME CHAR(15)');


You could probably also do it with a DBI call... but it would be much more difficult.
0
 

Author Comment

by:Erik N
ID: 1347500
Sorry Doldrum, SQL-statements is like chinese for me. I would like to do this without using SQL, if possible....

To ZifNab: I appreciate it !!

Thanx !!
Erik N.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1347501
Sorry, at the moment I can't find an easy solution. But may I ask why you need a field to a table? Why don't you just use a calculated field?
At the moment : If you don't want to use SQL, you can create a totally new table with your new field and then use the TBatchmove component in mode batUpdate. To copy the records for your first table to your new table.
But still, I'd like to have an answer to my quistion, if it's possible.
0
 

Accepted Solution

by:
essbb earned 40 total points
ID: 1347502
You need to use the batchmove component.

Follow these steps

1 create a temporary table with the new field
2 Use Batchmove to copy the existing records into the temp table
3 delete original table
4 rename temporary table to original table.

This could be dangerous in a multi-user environment.
If you need multiuser support use the SQL
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347503
essbb,
I don't know if you know it, but your answer looks very much like my comment. I hope you didn't saw this, because otherwise I get strange feelings about it.

0
 

Expert Comment

by:essbb
ID: 1347504
No I didn't see your comment last time. If you ment is as an answer you should have indicated this and the system would have locked me out.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347505
essbb, didn't gave it as an answer because it's not really a beautifull answer. But if he likes it, he could use it.
Have fun,
ZifNab;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347506
Erik N!
Sorry, I made a mistake! It's possible, I'll give you the function: DbiDoRestructure.
Want more information, just ask!
c.u. ZifNab;

0
 

Author Comment

by:Erik N
ID: 1347507
Thank you essbb for your answer!

I would also like to thank ZifNab for his comments..
(I want the user to be able to add a field to the table in order to adapt the application to new situations. So the application won't be crap the minute the situation changes.)


I guess the only right thing to do here is to use SQL after all...

Erik N
0
 

Author Comment

by:Erik N
ID: 1347508
ZifNab!
I really would like the info about DbiDoStructure (if possible)!!
Hope you see this..

Thanx!
Erik N
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question