| | 36 | |
| | 37 | Sample Perl script that gets data from the database: |
| | 38 | {{{ |
| | 39 | #! /usr/bin/perl |
| | 40 | # |
| | 41 | # Script: getdata.pl |
| | 42 | # A simple script that gets all the rows from a single table in the database. |
| | 43 | # |
| | 44 | # ./getdata.pl <db_name> <table_name> <outputfile> |
| | 45 | # |
| | 46 | # Example: ./getdata.pl zmac1_2005_04_28_00_46_10 sender_otg_senderport out.txt |
| | 47 | # |
| | 48 | # To use this script replace the XXXX in DBUSER and DBPASS |
| | 49 | # with correct username and password for idb1. |
| | 50 | # |
| | 51 | # |
| | 52 | use DBI(); |
| | 53 | |
| | 54 | $DBHOST = "idb1"; |
| | 55 | $DBNAME = $ARGV[0]; |
| | 56 | $DBUSER = "XXXX"; |
| | 57 | $DBPASS = "XXXX"; |
| | 58 | $QUERY = "select * from $ARGV[1]"; |
| | 59 | $OUTFILE = $ARGV[2]; |
| | 60 | |
| | 61 | $dsn = "DBI:mysql:database=$DBNAME;host=$DBHOST"; |
| | 62 | |
| | 63 | #Connect to the DB |
| | 64 | $dbh = DBI->connect($dsn, $DBUSER, $DBPASS, {'RaiseError' => 1}); |
| | 65 | |
| | 66 | # Prepare and execute query |
| | 67 | my $qry = $dbh->prepare($QUERY); |
| | 68 | $qry->execute(); |
| | 69 | |
| | 70 | open(out, ">$OUTFILE"); |
| | 71 | |
| | 72 | #Print the column names |
| | 73 | print out "@{$qry->{'NAME'}} \n"; |
| | 74 | |
| | 75 | #Print the data |
| | 76 | while (my @ref = $qry->fetchrow()) { |
| | 77 | print out "@ref \n"; |
| | 78 | } |
| | 79 | |
| | 80 | $qry->finish(); |
| | 81 | # Disconnect from the database. |
| | 82 | $dbh->disconnect(); |
| | 83 | }}} |