Solved

Adding new FIELD to database

Posted on 1998-06-21
5
151 Views
Last Modified: 2010-04-04
Hi all.

Is it possible to add a new FIELD to a Paradox database?  I have a table which looks like this:

Name: String
ID:   Integer
Word: String

Now, I want to add another field to this table at run-time.  It has to be an integer field. So we would now have

Name: String
ID:   Integer
Word: String
Ref:  Integer

Any ideas?  The reason it has to be done like this is because I need to add a new field for each user.  There could be as few users as 20 and as many as 500.

Stuart.
0
Comment
Question by:Stuart_Johnson
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
kjteng earned 50 total points
ID: 1354993
You have to create a new table and use tableN.fieldDefs.add method to add new field definition. After that copy all you existing data to the new table and rename it. (there is an example in delphi help). It is easier to use SQL to create new table if you are familier with SQL.

BTW I wonder why you need to add one field for each user. Something is not wrong with your database design. It appears to me that your database is not normalised. Try to normalise it and you can avoid adding indefinite number of field at runtime.  

0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1354994
Thanks for the suggestion.  Just so you know, the reason we want to add a new field for each person is this.  We have a class of 20 students (this could vary as I mentioned before, upto at least 500).  We have a list of over 8700 words which we need to teach to these students.  We need to assign each student one record for each word with a status on that word.

For example.

WORD   STUDENT1   STUDENT2
------|---------|----------|--- >
EGG   | 1       | 3  
BIN   | 0       | 2
GREEN | 2       | 0

Etc.  Now, can you see why we want to do it this way?  The other way would be too slow and difficult.

Can you give me info on how to do that with SQL?

Cheers!


Stu.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1354995
Create an event to add field: something like this

procedure TForm1.Button1Click(Sender: TObject);
begin
{What the following code does is actually same as this SQL statement:  alter table "my.db" add newItem integer}
  with query1 do begin
    close;
    sql.clear;
    sql.add( 'alter table ''cat.db'' add newItem integer');
    execSql
  end;
end;

That simple! That's why I love Delphi :-)



0
 
LVL 2

Expert Comment

by:kjteng
ID: 1354996
if the Ref is not more than 255, you may then do this:

1. create two string fields, namely s1_255 and str256_511
2. intilised the value of both field to '0000...0' ie 255 times of zero
3. if ref value of student with number 1 to 255 change, just assign the new value to  1st character of s1_255, something like this
   a:= table1.fieldbyname( 's1_255').asString;
   a[n]:= 'a'   { n = 1..255}
4. similarly for student number higher than 255:
   b:= table1.fieldbyname( 's256_511').asString;
   b[n-255]:= 'b' { n = 256..511}
  etc
5  to show the ref for student n, showMessage( b[n-255])
Will this work for you?

Bye.
Will come back 8 hours later.
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1354997
Excellent!! Wonderful!  Thanks for that! I used the SQL method.  Neater and quicker.

Stu.
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
Machine not responding during CopyFile() 3 89
Delphi XE10, MySQL Query 4 127
Print Graphic and Text to Epson TM-T88v 12 185
Delphi and Access based Enumeration 9 48
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now