...

View Full Version : Deleting part of a database



scchapman
08-30-2002, 11:58 PM
I was wondering if anyone knew how to delete some data from a flat-file database without it deleting any of the other data.

Jeepers
08-31-2002, 01:05 AM
Load the database into an array. Run a query to find the data that is not required saving the required stuff to another array and not saving the unrequired stuff. Then save the new array data to the database file.

scchapman
08-31-2002, 06:27 AM
I am pretty much a novice at CGI script, so I really don't have the knowhow to do what you just said. I'll try to give you an example of what I am working with though so that maybe you could give me a general CGI code that I could work with.

First of all my flat-file database is a text file. It contains the following information:

Twister|Action|PG-13|0|Available
The Patriot|Action|R|0|Available
Extreme Days|Comedy|PG|0|Available

This database will have more listings added to it at anytime, so the code needs to be somewhat flexable. I do not understand how to load one line in one array with with the other lines being loaded into another, along with saving the new array to the database all in alphabetical order. I know this seems to be a bit much, but I am trying to learn how this stuff works.

Mouldy_Goat
08-31-2002, 10:32 PM
*sigh* I actually wrote quite a beautiful little flatfile database editor, but managed to delete it before uploading for testing.

If I want to delete an item from a flatfile database I use the 'splice' command, which can selectively remove elements in an array.

The syntax goes, splice ARRAY, OFFSET, LENGTH, REPLACE-LIST
For example:

splice(@array, 0, 1);

Would delete 1 element, from the 0th element onwards... i.e. the first element.

Similarly,

splice(@array, 3, 2);

Would delete elements 3 and 4 from the array @array.

So for a flatfile database, you could do something like this:

#!/usr/bin/perl

$db = "flatfile.db";
chomp ($line_to_delete = <STDIN>);

open (DB, "$db") || die "Couldn't open $db for reading: $!";
@lines = <DB>;
close DB;

splice (@lines, $line_to_delete - 1, 1);
# the -1 there is because the 4th element in a list is referenced
# by '3' etc.. don't ask me who thought it was a good idea to
# start arrays with 0, but we're stuck with it now..

# this clobbers the contents and replaces them with the updated
# @lines array
open (DB, ">$db") || die "Couldn't open $db for writing: $!";
print DB @lines;
close DB;
print "Done\n";

So, if The Patriot copy had a nasty accident with a video recorder, you could delete its entry in the DB like so:

#!/usr/bin/perl

chomp($title_to_delete = <STDIN>);

open (DB, "$db") || die "Couldn't open $db for reading: $!";
@lines = <DB>;
close DB;

for ($i = 0; $i <= $#lines; $i++) {
# this line is a neat little trick for getting the title from the db
# i'll explain it afterwards..
($title = $lines[$i]) =~ s/\|.*//;
if ($title eq $title_to_delete) {
splice (@lines, $i, 1);
}
}

open (DB, ">$db") || die "Couldn't open $db for writing: $!";
print DB @lines;
close DB;
print "Done\n";

This line:
($title = $lines[$i]) =~ s/\|.*//;

Means this:
Copy the contents of the current line into $title, then get rid of everything after and including the first pipe found in it.

And as for arranging the database in alphabetical order.. well there's a great module called Sort::Fields which will do it for you. You can read about it here (http://search.cpan.org/author/JNH/Sort-Fields-0.90/Fields.pm)

You could use it like this:

#!/usr/bin/perl

open (DB, "$db") || die "Couldn't open $db for reading: $!";
@lines = <DB>;
close DB;

@lines = fieldsort '\|', [0], @lines;

open (DB, ">$db") || die "Couldn't open $db for writing: $!";
print DB @lines;
close DB;
print "Done\n";

Feel free to ask any questions...

scchapman
09-01-2002, 04:12 AM
I really wish you had not deleted that file for the flat-file database. What I thought was so simple has become a nightmare for me. All I wanted to do was create a database based on several things: a movie title, the rating, the genre, the status of the movie (if it was checked out or not), and display the person who checked it out. I then wanted to make a simple CGI code that could search through the database for anything, modify the database to change things, and add movies to the database, all through HTML files. Even with all the help I have received I still cannot figure this one out. I am desperate for a full cgi code that does all this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum