Solved

Add a column to db-tables

Posted on 1998-05-25
15
269 Views
Last Modified: 2010-04-06
I a couple of tables and have to add another Column to the existing table, and then save the table with the new Column, how do I do that, btw I'm using Delphi 2.0.
0
Comment
Question by:koger
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 1

Expert Comment

by:jturpin
ID: 1347738
The best way to do that is by using the
"Borland Database Desktop" under "Tools"
(I think) on the main Delphi2 menu.

John.
0
 

Author Comment

by:koger
ID: 1347739
Ehh, well, I need to create a program that can add a column, so what I need is some code ;-)
0
 
LVL 1

Expert Comment

by:jturpin
ID: 1347740
Ok you seem to have missed that point in
your original question.

I don't believe you can simply add a column
to a table. I usually clone the structure
of the original table, add the column, and then
select the info I need. But you could try
simply adding the column to the existing table
if you want, although I think you will get
an error.

The code for adding a column to the table is:

   OriginalTable.FieldDefs.Add(FieldName,
      FieldType,FieldLen,false);

   e.g.
      Table1.FieldDefs.Add('SupCarry',ftString,SupLEN,false);
      Table1.FieldDefs.Add('PayCarAll',ftBoolean,0,false);
      Table1.FieldDefs.Add('Rate',ftFloat,0,false);
      Table1.Exclusive:= true;
   
      Table1.CreateTable;

I hope that helps,

John.
0
 

Author Comment

by:koger
ID: 1347741
To jturpin

Here is a ex. of the code I use:

Table1.FieldDefs.Add('NewEntry:', ftString, 25, False);
Table1.CreateTable;

This will causs all the existing information in the table to be erased :-(, how is it possible to keep the existing information and still add an extra column?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1347742
Use SQL. For adding a new field, called 'TEST' to the ANIMALS table write:

with Query1 do
begin
  DatabaseName:='DBDEMOS';
  SQL.Text:='ALTER TABLE "ANIMALS.dbf" ADD TEST SMALLINT';
  ExecSQL;
end;

0
 

Author Comment

by:koger
ID: 1347743
It must be possible to add a column, without using SQL.
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1347744
You need to use the dbiDoRestructor procedure. There is an example at http://www.inprise.com/devsupport/bde/bdeapiex/dbidorestructure.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:koger
ID: 1347745
Ok I have tried it all, and I simply don't get it, so I will take it one more time

I have to add a column to a paradox table, I need some code for Delphi 2.0 that does the trick.

The existing columns was made with fielddefs

with Table1.FieldDefs do
begin
  Clear;
  Add('Column1', ftString, 50, False);
  Add('Column2', ftString, 50, False);
end;
Table1.CreateTable;

if I try to add another column after table1.createtable was used,
I get an error.

Table1.FieldDefs.Add('Field3', ftString, 25, False);
Table1.FieldDefs.Update;
DBEdit3.DataField := 'Field3';  <-- error 'Field3 dosn't exist!'

If I call Table1.CreateTable again the whole table will be erased, all the information written in the table are lost.

Would someone give me an example that matches my table?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1347746
What's wrong with SQL?
0
 
LVL 1

Expert Comment

by:Marcius
ID: 1347747
The use of fielddefs will not create a row in the underlying table. It is an operator designed to manipulate TDataset, not the underlying table.

I really think that the best solution is to use SQL. Other than that, you can re-create a new table each time you want to add a column, migrate the data, then destroy the old table. As you can see, SQL is the cleaner option.
0
 

Author Comment

by:koger
ID: 1347748
Ok, how do I use SQL?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1347749
I have already posted the answer on SQL and you rejected it.
0
 

Author Comment

by:koger
ID: 1347750
To Ronit.

The reason that I really didn't want(ed) to use SQL, is that my application don't use SQL and I haven't used SQL before.

you gave me this SQL statement,

  SQL.Text:='ALTER TABLE "ANIMALS.dbf" ADD TEST SMALLINT';

I need a string field with the length 25, I haven't got any manual that covers SQL statements, so it is a bit tricky to figure out how the statement should look, will help me with that?
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 100 total points
ID: 1347751
This must be done exclusively, meaning the table cannot be active in your application or another application, while SQL is executing:

with Query1 do
begin
    DatabaseName:='DBDEMOS';
    SQL.Text:='ALTER TABLE "ANIMALS.dbf" ADD TEST CHAR(25)';
    ExecSQL;
 end;

for more information on SQL statements see:
C:\Program Files\Borland\Common Files\BDE\Localsql.hlp
0
 

Author Comment

by:koger
ID: 1347752
Thanx!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 91
Delphi Dbf export problem to a Visual Foxpro application 6 161
Printing problem 2 80
LAN or WAN ? 11 81
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now