Hello, ODBC(Perl) World!
Posted on 9月 20th, 2012 by cx20
ODBC
ODBC(Open Database Connectivity)は、マイクロソフト社が提唱した DBMS 接続用の API 仕様である。
DBMS の差異は ODBC ドライバによって吸収される為、ODBC の手順にしたがってプログラムを作成すれば、基本的な差異を意識せず、プログラムすることができる。
ODBCドライバ |
ファイル |
Microsoft Access Driver (*.mdb) |
ODBCJT32.DLL |
Microsoft Text Driver (*.txt; *.csv) |
ODBCJT32.DLL |
Microsoft Excel Driver (*.xls) |
ODBCJT32.DLL |
Microsoft dBase Driver (*.dbf) |
ODBCJT32.DLL |
Microsoft ODBC for Oracle |
MSORCL32.DLL |
Microsoft Paradox Driver (*.db ) |
ODBCJT32.DLL |
SQL Server |
SQLSRV32.DLL |
Microsoft Access Driver (*.mdb, *.accdb) |
ACEODBC.DLL |
SQL Server Native Client 10.0 |
SQLNCLI10.DLL |
ソースコード(Perl + ODBC + SQL Server)
use strict;
use Win32::ODBC;
my $db = Win32::ODBC->new('Driver={SQL Server};Server=(local);UID=sa;PWD=P@ssW0rd');
$db->Sql("SELECT 'Hello, ODBC World' AS Message");
while ($db->FetchRow) {
my %row = $db->DataHash;
print "Message", "n";
print "-------------------n";
print $row{Message}, "n";
}
$db->Close(); |
use strict;
use Win32::ODBC;
my $db = Win32::ODBC->new('Driver={SQL Server};Server=(local);UID=sa;PWD=P@ssW0rd');
$db->Sql("SELECT 'Hello, ODBC World' AS Message");
while ($db->FetchRow) {
my %row = $db->DataHash;
print "Message", "n";
print "-------------------n";
print $row{Message}, "n";
}
$db->Close();
ソースコード(Perl + ODBC + MySQL)
use strict;
use Win32::ODBC;
my $db = Win32::ODBC->new('Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd');
$db->Sql("SELECT 'Hello, ODBC World' AS Message");
while ($db->FetchRow) {
my %row = $db->DataHash;
print "Message", "n";
print "-------------------n";
print $row{Message}, "n";
}
$db->Close(); |
use strict;
use Win32::ODBC;
my $db = Win32::ODBC->new('Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd');
$db->Sql("SELECT 'Hello, ODBC World' AS Message");
while ($db->FetchRow) {
my %row = $db->DataHash;
print "Message", "n";
print "-------------------n";
print $row{Message}, "n";
}
$db->Close();
実行方法(Windows)
実行結果
Message
------------------
Hello, ODBC World! |
Message
------------------
Hello, ODBC World!
Tags: ODBC
Categories: ODBC, Perl