Solved

Adding new FIELD to database

Posted on 1998-06-21
5
149 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

11 Experts available now in Live!

Get 1:1 Help Now