Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting part of a database

    I was wondering if anyone knew how to delete some data from a flat-file database without it deleting any of the other data.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Conwy. UK
    Posts
    122
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by scchapman; 08-31-2002 at 05:31 AM.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *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:
    Code:
    #!/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:
    Code:
    #!/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

    You could use it like this:
    Code:
    #!/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...

  • #5
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •