Faircube Networks - Custom programming projects, internet consulting, web design, and various other multimedia and graphic design solutions.
 
Home arrow MySQL arrow Connect, create table and select with Perl (Linux)
Connect, create table and select with Perl (Linux)

   Connect, create table and select with Perl (Linux).  First the DBI module is needed, which
   can be installed from the system prompt as follows:

   # perl -MCPAN -e shell
   cpan> install DBI
   cpan> install DBD::mysql

   The following is an example program:




  #! /usr/bin/perl -w
  #  Copyright (GPL) Mike Chirico This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
  #
  #  Program does the following:
  # o connects to mysql
  # o creates perlTest if it doesn't exist
  # o inserts records
  # o selects and displays records
  #
  #  This program assumes DBI
  #
  #  perl -MCPAN -e shell
  #  cpan> install DBI
  #  cpan> install DBD::mysql
  #
  #
  #
  #
  #

  use strict;
  use DBI;

  #  You will need to change the following:
  # o database
  # o user
  # o password
  my $database="yourdatabase";
  my $user="user1";
  my $passwd="hidden";
  my $count = 0;
  my $tblcreate= "
CREATE TABLE IF NOT EXISTS perlTest (
  pkey int(11) NOT NULL auto_increment,
  a int,
  b int,
  c int,
  timeEnter timestamp(14),
 PRIMARY KEY  (pkey)

 ) ";

  my $insert= "
  insert into perlTest (a,b,c)
   values (1,2,3),(4,5,6),(7,8,9)";

  my $select="
  select a,b,c from perlTest ";



  my $dsn = "DBI:mysql:host=localhost;database=${database}";
  my $dbh = DBI->connect ($dsn, $user, $passwd)
  or die "Cannot connect to server\n";



  my $s = $dbh->prepare($tblcreate);
 $s->execute();
 $s = $dbh->prepare($insert);
 $s->execute();



 $s = $dbh->prepare($select);
 $s->execute();


  while(my @val = $s->fetchrow_array())
  {
  print " $val[0]  $val[1]  $val[2]\n";
  ++$count;
  }
  $s->finish();

  $dbh->disconnect ( );

  exit (0);

 

 

 
 
Home | Services | Portfolio | Support | Prices | About Us | Privacy | Terms | Website Resources | Legal
Copyright © 1999-2007 Faircube Networks Inc. All Rights Reserved.