Gossamer Forum
Home : Products : DBMan : Customization :

Changing DB layout: Adding fields

Quote Reply
Changing DB layout: Adding fields
Heya JPD,

Here's one: how do I go about changing the layout of my database after it's been running for a while and has some entries already?

For instance, how do I add additional database fields in between the fields that I've already defined? How do I add fields on to the back of the database?

I don't think this is a s easy as only updating and changing the %db_def in the cfg file. I'll have to find a way to squeeze some delimiters into the text file. Is there an easy way?



------------------
Safe swoops
Sangiro

http://www.dropzone.com/
Quote Reply
Re: Changing DB layout: Adding fields In reply to
The easiest way is to add the fields onto the end of the records. Open your .db file in a text editor and add an extra delimiter on each line for each new field you're going to add.

You can add them to the middle of the records, but it's pretty tough to keep everything straight so you know where you're adding the field. If you really need to add a field in the middle, my suggestion is to import the database into something like Access, add the field and then export the database again.


------------------
JPD





Quote Reply
Re: Changing DB layout: Adding fields In reply to
Well, Carol got there first with her suggestion, but here's another way of doing it...


This'll open and read the old database, add a delimiter to the end of each line and rewrite it to a new file.

Code:
#!/perl/bin/perl.exe
# Don't forget to change the path to Perl!

print "Content-type: text/plain\n\n";

# Change the path here to your old DB.
open(FILE,"/path/to/old.db") or print "Can't open old file for reading.\n\n";
@lines = <FILE>;
close(FILE);

foreach $line (@lines) {
chomp $line;
$newlines .= "$line\|\n";
}

# Change the path here to your new DB.
open(FILE,">/path/to/new.db") or print "Can't open new file for writing.\n\n";
print FILE $newlines;
close(FILE);

print "Done!\n\n";

# Uncomment this if you want to see the data that was written to the new file.
#print "$newlines";

exit;

If you want to put the new field at the start, just change:

$newlines .= "$line\|\n";

to:

$newlines .= "\|$line\n";

Placing it in the middle would be a little more awkward, but can be done. I'd need your fields to do it.

Cheers,
adam
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Thanks guys!

Both these replys are very useful. I'm a bit of an ignoramus with databases so I guess the question that I should be asking is this:

Is there any reason why I would rather add a field in the middle of a record than at the back? Having thought about it a bit after my most, me thinks it probably doesn't matter where the field gets added .....



------------------
Safe swoops
Sangiro

http://www.dropzone.com/
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Right. No reason you need to add a field to the middle. You can have the input form and display of the field anywhere you want.


------------------
JPD





Quote Reply
Re: Changing DB layout: Adding fields In reply to
dahamsta,

I'm trying to set up this little cgi script you posted. Cut-n-paste didn't work (it all ended up on one line) so I've got to retype it.

It doesn't want to execute so I'm debugging a bit.

The > character in line 13 in the open command, what is that for? Got this available somewhere in the web in txt format?




------------------
Safe swoops
Sangiro

http://www.dropzone.com/
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Hiya,

Well, it *should* work, because I tested it locally, but that said, nothing's perfect. It needs to be chmod'd to 755, and it creates and writes to a new file, so the directory you're writing to needs to be chmod'd 777. Alternatively, create a file called new.db or whatever, and chmod that 777. Make sure your path to Perl and the path to the old and new DB's are correct.

Here's a text copy on the web.

dotcgi.com/dbman/addfield.cgi.txt

The > is telling the script to create or overwrite the file. >> will append to the end of a file, or < will read it, although that's optional. So if you wanted to overwrite the old file, use >>, but make sure you backup first!

Good luck!

adam

[This message has been edited by dahamsta (edited May 20, 1999).]
Quote Reply
Re: Changing DB layout: Adding fields In reply to
adam,

I'm spinning my wheels on this. I've done as you said. maid sure the permissions on both the directories and files have been set correctly. Created a new.db file and set it to 777.

When I run the script with my Perl line pointing at Perl4 it simply give me a HTTP 500 - Internal server error. When i run it pointing at Perl5 it tries to download the file!

Here's my script if you don't mind taking a peek:

http://www.dropzone.com/addfields.cgi.txt

Hope you can help.

------------------
Safe swoops
Sangiro

http://www.dropzone.com/
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Well, it seems unlikely, but possibly text/plain isn't a recognisable mime type on your server.

Leave it pointing to Perl5 and change:

print "Content-type: text/plain\n\n";

to:

print "Content-type: text/html\n\n";

If that doesn't work, I'll try lobbing it on my own remote server and seeing if it works there. Maybe you'd have been better of going JPDeni's route in the first place... Smile

adam

ps. Just tried it remotely and it works ok!

[This message has been edited by dahamsta (edited May 21, 1999).]
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Adam

I got this to work - thanks. Another question. If I wanted to add 5 fields to my DB. Could I change the code like hereunder and safely run the script only once?

$newlines .= "$line\&#0124; &#0124;&#0124; &#0124;|\n";


------------------
Safe swoops
Sangiro

http://www.dropzone.com/
Quote Reply
Re: Changing DB layout: Adding fields In reply to
Hmmm...

Basically, yes, but I'm not sure about your syntax. I think that you might need to escape each instance of the pipe symbol, so's not to confuse Perl. Like this:

$newlines .= "$line\|\|\|\|\|\n";

Try it that way, but make sure you backup first. I know it's writing to a new file, but better to be safe than sorry. Especially with my error prone scripts...

Smile

adam

[This message has been edited by dahamsta (edited June 10, 1999).]