基于湖库一体架构,统一管理结构化、半结构化与非结构化等多模态数据,一个系统承载事务处理、实时分析与 AI 工作负载。
Perl 如何连接 OceanBase 数据库
更新时间:2026-06-03 03:01
本文用一个简单示例描述 Perl 语言如何连接 OceanBase 数据库。
适用版本
OceanBase 数据库 V2.x 和 V3.x 版本
操作步骤
Perl 语言的 DBI 集成了 Oracle、MySQL、Informix 等数据库的驱动。
- Perl 连接 OceanBase 数据库 MySQL 模式是可以的,因为 OceanBase 数据库 MySQL 模式支持原生 MySQL 的驱动。
- Perl 连接 OceanBase 数据库 Oracle 模式是不行的,因为 OceanBase 数据库 Oracle 模式不能采用 Oracle 的驱动。 以下为一个简单的示例:
#!/usr/bin/env perl
use strict;
use warnings;
use DBI();
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=xxx.xxx.xxx.xxx;port=45447",
"admin", "admin",
{'RaiseError' => 1});
# Drop table 'foo'. This may fail, if 'foo' doesn't exist.
# Thus we put an eval around it.
$dbh->do("DROP TABLE IF EXISTS foo");
# Create a new table 'foo'. This must not fail, thus we don't
# catch errors.
$dbh->do("CREATE TABLE foo (id INTEGER primary key, name VARCHAR(20))");
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");
# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");
# Now retrieve data from the table.
my $sth = $dbh->prepare("SELECT * FROM foo");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'id'}, name = $ref->{'name'}\n";
}
$sth->finish();
# Disconnect from the database.
$dbh->disconnect();