Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Inserting, update and deleteting a database under conditions

by Nik
on Jan 09, 2008 at 12:22 UTC ( #661326=perlquestion: print w/replies, xml ) Need Help??
Nik has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'am trying to perl code the following ideas i have commented as remarks but my attempts were fruitless.
Its complicated and although i tried to work some code, my code does not do what the remarks say.
my @files = glob "$ENV{'DOCUMENT_ROOT'}/data/text/*.txt"; my @menu_files = map m{([^/]+)\.txt}, @files; my @dbfiles = $db->do( 'SELECT title, body FROM articles' ); my $replace = $db->prepare( 'INSERT INTO article (title, body) VALUES +(?, ?)' ); foreach ( @menu_files ) { #Check every filename(item in @menu_files) as well as its content t +o see if is already inserted in table articles under columns title an +d body #============ INSERT NEW FILENAMES & CONTENTS INTO DATABASE IF ANY +============= #If filename and content don't already exist in the db then do inse +rt them #===== UPDATE THE DB ENTRY IF THERE IS A MODIFICATION IN A FILES C +ONTENTS ===== #If filename exists in the db under column title but its content is + different from the database's content then update column body in the + db #======== DELETE ANY DB ENTRY OF A FILENAME THATDOESNT EXIST ANY M +ORE ========= #Delete all db records that doesnt have a corresponding filename in + the hdd }

Replies are listed 'Best First'.
Re: Inserting, update and deleteting a database under conditions
by apl (Monsignor) on Jan 09, 2008 at 12:40 UTC
    You haven't included any code, so it's difficult to comment on why your attempts were fruitless and your code (did) not do what the remarks say.

    You seem to have the program broken down into six reasonable components. Why not pick one or more, show us what you've done, and tell us what resulted?
      well i didn't go far and the code apart form logical errors has syntax errors as well, its a total mess.
        Most of the Monks, myself included, will be more than happy to help you, but I doubt any of us will write the entire program for you.

        If you give us the syntax errors, we can help you resolve those. If you show us the database code, and tell us what the logical errors were, we can point you in the proper direction. Otherwise, ...
Re: Inserting, update and deleteting a database under conditions
by moritz (Cardinal) on Jan 09, 2008 at 12:46 UTC
    my @menu_files = map m{([^/]+)\.txt}, @files;

    Your @menu_files will be rather boring - 0 or 1.

    You should use grep instead of map.

    Update: wait, that's crap... but on the other hand the result might still not be what Nik expects - dunno

      Actually, it would be an array of '' (the empty string) or 1 -- but still boring.
      The results of map are not 0 or 1, @menu_files actually stores the filenames from the strign with the path and txt
      . How could i write the same thing with grep?
        my @menu_files = grep m{([^/]+)\.txt}, @files;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://661326]
Approved by Corion
NodeReaper serves mincemeat tarts with the cider

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-10-21 11:19 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (269 votes). Check out past polls.