如何使用DBIx :: Simple + SQL :: Abstract与DBIx :: Connector

时间:2013-12-03 14:41:08

标签: mysql perl dbi

我在我的应用程序中使用DBIx :: Simple with SQL :: Abstract。这是数据库连接代码:

my $nms_host    = 'mysql.server.com';
my $nms_base    = 'mydb';
my $nms_user    = 'myuser';
my $nms_pass    = 'password123';
my $nms_dsn     = "DBI:mysql:$nms_base:$nms_host";

# Initialize connection
# NMS
my $db_nms = DBIx::Simple->connect($nms_dsn, $nms_user, $nms_pass, {
    RaiseError => 1,
    mysql_enable_utf8 => 1
}) or die DBIx::Simple->error;
$db_nms->abstract = SQL::Abstract->new(
    case    => 'lower',
    logic   => 'and'
);

现在我需要使用DBIx :: Connector,但我不明白应该如何建立连接。

1 个答案:

答案 0 :(得分:1)

DBIx :: Connector创建数据库句柄,然后可以直接将其传递给DBIx :: Simple。因此,出于您的目的,这样的事情应该有效:

#!/usr/bin/env perl

use strict;
use warnings;

use DBIx::Connector;
use DBIx::Simple;

my $nms_host    = 'mysql.server.com';
my $nms_base    = 'mydb';
my $nms_user    = 'myuser';
my $nms_pass    = 'password123';
my $nms_dsn     = "DBI:mysql:$nms_base:$nms_host";

my $conn = DBIx::Connector->new(
    $nms_dsn,
    $nms_user,
    $nms_pass,
    {   RaiseError        => 1,
        mysql_enable_utf8 => 1,
    }
);

my $simple = DBIx::Simple->connect( $conn->dbh );