通过dbi使用perl连接mysql数据库的方法

通过dbi使用perl连接mysql数据库的方法,第1张

通过使用DBI,用Perl可以很容易的连接到mysql数据库:

代码如下:

复制代码

代码如下:

#!/bin/perl

use

DBI

#

Connect

to

target

DB

my

$dbh

=

DBI->connect("DBI:mysql:database=eyglehost=localhost","username","password",

{'RaiseError'

=>

1})

#

Insert

one

row

my

$rows

=

$dbh->do("INSERT

INTO

test

(id,

name)

VALUES

(1,

'eygle')")

#

query

my

$sqr

=

$dbh->prepare("SELECT

name

FROM

test")

$sqr->execute()

while(my

$ref

=

$sqr->fetchrow_hashref())

{

print

"$ref->{'name'}n"

}

$dbh->disconnect()

Step 1:安装FreeTDS模块

这是个Sybase的Client端软件,必须安装。可以从www.freetds.org/software.html上下载源文件安装程序FreeTDS-stable.tgz来进行安装。

可能需要在系统上增加如下的环境变量: LD_LIBRARY_PATH中增加/usr/4lib

安装步骤:

安装FreeTDS

#gunzip FreeTDS-stable.tgz

#tar xvf FreeTDS-stable.tar

#cd freetds-0.63 目前版本是0.63

#./configure --prefix=/usr/local/freetds

#make

#make install

配置FreeTDS

#cd /usr/local/freetds/etc

修改配置文件freetds.conf,在其中增加需要连接的Sybase服务器相关信息,如下:

[TEST]

host=192.168.0.1

port=4100

tds version = 5.0

Step 2:安装和配置DBI-1.50 、DBD-Sybase-1.07 、sybperl-2.18等Module即可。可以从www.cpan.org上下载。

修改CONFIG文件SYBASE配置路径,将其设置为/usr/local/freetds

设置环境变量:

SYBASE=/usr/local/freetds

export SYBASE

LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$SYBASE/lib

export LD_LIBRARY_PATH

# Fix dbdimp.c before compiling or you will get an error:

>dbdimp.c:800: error: 'BLK_VERSION_150' undeclared (first use in this function)

>dbdimp.c:800: error: (Each undeclared identifier is reported only once

>dbdimp.c:800: error: for each function it appears in.)

# This is really a FreeTDS bug - they define the CS_VERSION_1xx symbols, but not the corresponding BLK_VERSION_xxx symbols.

Edit dbdimp.c and add

vi /usr/src/DBD-Sybase-1.08/dbdimp.c

#define BLK_VERSION_150 BLK_VERSION_100

#define BLK_VERSION_125 BLK_VERSION_100

#define BLK_VERSION_120 BLK_VERSION_100

vi /download/sybperl-2.19/CTlib/CTlib.c

#define BLK_VERSION_150 BLK_VERSION_100

#define BLK_VERSION_125 BLK_VERSION_100

#define BLK_VERSION_120 BLK_VERSION_100

连接示例:

#!/usr/bin/perl

#

use DBI

use Sybase::DBlib

Sybase::DBlib::DBSETLCHARSET("eucgb")

Sybase::DBlib::DBSETLNATLANG("chinese")

$dbh = Sybase::DBlib->new('root', '1234', 'TEST')

$dbh->dbcmd("select * from test.name")

$dbh->dbsqlexec

while($dbh->dbresults != NO_MORE_RESULTS) {

while(@data = $dbh->dbnextrow) {

print @data,"\n"

}

}

$dbh->dbclose


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6776478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存