---
title: "perl操作mysql数据库"
author: "luogen33"
date: "2011-09-26T23:06:46Z"
tags: []
categories: ["编程语言"]
slug: "perl-mysql"
draft: false
lastmod: "2011-09-26T23:18:29Z"
id: 1181046
url: "https://luogen33.iteye.com/blog/1181046"
---

#!/usr/bin/perl -w
use DBI;
my $driver="DBI:mysql";
my $database="test";
my $user="root";
my $host="localhost";
my $passwd="luogen30";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user;password=$passwd")
    or die "Can't connect: " . DBI->errstr;
    # 操作多列 
    my $sth=$dbh->prepare("select * from testtable ");
#操作一列 
#my $sth=$dbh->prepare("SELECT NAME FROM testtable")
#   or die "Can't prepare sql statement" . DBI->errstr;

$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
 # 操作多列 
while(@recs=$sth->fetchrow_array){
    print $recs.":".$recs.":".$recs."\n";
    }
#操作一列 
#my($name);
#$sth->bind_columns(\$name);
#while( $sth->fetch()){
#     printf $name ."\n";
#}

$sth->finish();
$dbh->disconnect();