Changes between Version 5 and Version 6 of Tutorials/a0Basic/Tutorial3


Ignore:
Timestamp:
Nov 16, 2005, 6:56:31 AM (19 years ago)
Author:
pkamat
Comment:

Added sample Perl script to retrieve data from idb1

Legend:

Unmodified
Added
Removed
Modified
  • Tutorials/a0Basic/Tutorial3

    v5 v6  
    3434[[Image(Matlabexample.PNG)]]
    3535
     36
     37Sample 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#
     52use 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
     67my $qry = $dbh->prepare($QUERY);
     68$qry->execute();
     69
     70open(out, ">$OUTFILE");
     71
     72#Print the column names
     73print out "@{$qry->{'NAME'}} \n";
     74
     75#Print the data
     76while (my @ref = $qry->fetchrow()) {
     77        print out "@ref \n";
     78}
     79
     80$qry->finish();
     81# Disconnect from the database.
     82$dbh->disconnect();
     83}}}