Guest User

Untitled

a guest
Aug 26th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Upload into database using Perl
  2. use warnings;
  3. use strict;
  4. use DBI;
  5. use HTML::TreeBuilder;
  6.  
  7. open (FILE, "demo") || die "couldn't open the file!";
  8. open (F1, ">demo.htm") || die "couldn't open the file!";
  9. open (F2, ">demo2.csv") || die "couldn't open the file!";
  10.  
  11. # database name, user and password
  12. my $data_source = q/dbi:ODBC:demo/;
  13. my $user = q/demo/;
  14. my $password = q/demo/;
  15.  
  16. # Connect to the data source and get a handle for that connection.
  17. my $dbh = DBI->connect($data_source, $user, $password)
  18. or die "Can't connect to $data_source: $DBI::errstr";
  19.  
  20. print F1 "Name|Lives In|Commentedn";
  21. print F2 "Name|Lives In|Commentedtextn";
  22.  
  23. my $tree = HTML::TreeBuilder->new_from_content( do { local $/; <DATA> } );
  24.  
  25. for ( $tree->look_down( 'class' => 'postbody' ) )
  26. {
  27. my $location = $_->look_down( 'class' => 'posthilit' )->as_trimmed_text;
  28.  
  29. my $comment = $_->look_down( 'class' => 'content' )->as_trimmed_text;
  30.  
  31. my $name = $_->look_down( '_tag' => 'h3' )->as_text;
  32.  
  33. $name =~ s/^Re:s*//; $name =~ s/s*$locations*$//;
  34.  
  35. print "Name: $namenLives in: $locationnCommented: $commentn"; }
  36.  
  37. # This query generates a result set with one record in it.
  38. #my $sql = "SELECT 1 AS test_col";
  39.  
  40. my $sql = "insert into demo2 values (Name, Lives In, Comeented, text)";
  41.  
  42. print $sql;
  43. print "n";
  44.  
  45. # Prepare the statement.
  46. my $sth = $dbh->prepare($sql)
  47. or die "Can't prepare statement: $DBI::errstr";
  48.  
  49. # Execute the statement.
  50. $sth->execute();
  51.  
  52. }
  53. print $b;
  54. print " endn";
  55.  
  56. # Disconnect the database from the database handle.
  57. $dbh->disconnect;
  58.  
  59. <div class="postbody"> <h3><a href "foo">Re: John Smith <span class="posthilit">England</span></a></h3> <div class="content">Is C# better than Visula Basic?</div> </div>
  60.  
  61. my $sql = "insert into demo2 values (?,?,?,?)";
  62. ...
  63. $sth->execute($name,$location,$comment,'');
Add Comment
Please, Sign In to add comment