6-1 資?庫整??

Perl MrjjBz\AYnaxsrA̤Kk٬ONrJƮw]Databases^CXGҦ UNIX tγѤ@M٬ DBM (Database Management) зǵ{wA]tFƮwإߡBŪBgJBR\AƮw榡O UNIX Mή榡AõLkM@Ʈw]p Oracle MS Access^qCPerl ]䴩 DBM {wA]]iHإߦۤvMΪƮwAѩ榡M@ Win32 Ʈw]p AccessBSQL ServerBFoxpro^LkqAҥHKC]`INH Perl ps Win32 Ʈw]p Microsoft AccessBSQL Server^DC

򥻤WAPerl Og ODBC (Open Database Connectivity) MƮwqCODBC OѼƭӤjƮwtөҭqwзǡA\NOѤ@ӧ㪺ε{]APIAApplication Program Interface^ALε{γngѤ@PөM󥭥xB䴩 ODBC ƮwqCƹWAPerl ٻݭn@MҲաA~M ODBC qAb Win32 xWA̱`ΪҲլO Win32::ODBCAoO Dave Roth ҳ]pҲաAM Perl s Win32 ƮwAiG

http://www.roth.net/odbc/
Perl{XBWin32::ODBC ҲաAHθƮw ODBC Driver YiΥkϨӪܡA䤤 Perl Is Win32::ODBC UؤkӡAWin32 AN SQL (Strucutre Query Language) ROeƮw ODBC Drivers Win32::ODBC Ҳը㦳UCuIG Perl bMƮwi淾qeAڭ̥]wuƨӷW١v]Data Source NameA² DSN^AoO@ӳsܸƮwNA]tUCTG DSN SOG ͷs System DSNAiѤUC覡ӹFG
}l/]w/x/ODBCƨӷ/tθƨӷW/sW
ۿƮwOΦmA̧ǧ]wYiCYͷs User DSNAuݱNWzutθƨӷW١v令uϥΪ̸ƨӷW١vYiC

@]w DSN AYiƮwiŪgʧ@C@ӨAPerl ƮwŪgʧ@iHpUG

  1. g DSN MƮwإ߳s
  2. eX SQL RO
  3. óBz SQL ROG
  4. ƤWzBJAsƧ
  5. Ʈws
إ߸Ʈws嫬{XpUG $DSN = "Address"; $db = new Win32::ODBC($DSN); # إ߸Ʈws Y DSN ]w]tFϥΪ̪bMKXAhWz{X[JbMKXG $DSN = "DSN=Address;UID=Jang;PWD=my_passwd"; $db = new Win32::ODBC($DSN); # إ߸Ʈws bWzdҤAbOuJangvAKXOumy_passwdvC

bإ߸ƮwsL{AYoͿ~AڭӦLX~TA]Wz{XĤGCiק令㪺gkApUG

if (!($db = new Win32::ODBC($dsn))) { print "Error in opening DSN \"$dsn\"!\n"; print "Reason: " . Win32::ODBC::Error(). "\n"; exit; } Ъ`NbWz{qAYƮws\Ah|^Ǥ@ӪAϤAh|^ undefAñN Win32::ODBC::Error() Ǧ^~TLXCsƮw\Aۧڭ̴NneJ SQL ROAi sql() kӹFA嫬{XpUG $sql = "SELECT * FROM table1"; # Xƪ table1 Ҧ if ($db->Sql($sql)) { print "Error in SQL query: \"$sql\"!\n"; print "Reason: " . $db->Error() . "\n"; $db->Close(); exit; } Ъ`NbWz{XAY SQL RO榨\Ah $db->sql($sql) |^ undefAoMezsƮw^ǷNqnAˡCY SQL ROɵoͥ~A$db->Error() |Ǧ^~TC

\eJ SQL ROAۧڭ̭nN^ǸƤ@@iBzA嫬{XpUG

while ($db->FetchRow()) { %Data = $db->DataHash(); K process result K } Wz while-j|@AAC\o@ƮɡA$db->FetchRow() YǦ^ 1Ap̧dzBzơAҦƳQBzCbCj餤A%Data |oCơAåH hash ƫAxsơA䤤ƮwWٴNܦ %Data keyAȴNܦ %Data valueCYuݭnƭAڭ̥iW٩J $db->DataHash ޼ơAҦpAYuno Name M Positoin ȡAiΤUC{XG %Data = $db->DataHash("Name", "Position"); bҦƪBzAOѤFnϥΤUC{XӲMMƮwsG $db->Close(); HUO@ӧ㪺ҤlAdҷ|N example/test.mdb ƮwvLXAbզdҤeAаOon]w DSNAYnNW٬ AddressBook DSN ]wܸƮw example/test.mdbCdҵ{X]listdb1.pl^pUG

lɡ]listdb1.pl^G]ǦϰUYi^
# Display a table in a database using PERL ODBC module

use Win32::ODBC;

$dsn = "AddressBook";
$table = "Namelist";

# ====== Check if the database can be opened correctly
if (!($db = new Win32::ODBC($dsn))) {
	print "Error in opening DSN \"$dsn\"!\n";
	print "Error: " . Win32::ODBC::Error(). "\n";
	exit;
}

# ====== Select all fields from the given table
$sql = "select * from $table;";
$sql = "select * from $table order by Category;";

# ====== Check if the SQL query is correct
if ($db->Sql($sql)) {
	print "Error in SQL query: \"$sql\"!\n";
	print "Error: " . $db->Error() . "\n";
	$db->Close();
	exit;
}

# ====== Print field data
print "Content-type: text/html\n\n";
print "<html><body><pre>\n";
$count = 1;
@FieldNames = $db->FieldNames();
while ($db->FetchRow()) {
	print "Record $count\n";
#	($newsSiteID, $classID) = $db->Data("newsSiteID", "classID");
#	undef %Data;
	%Data = $db->DataHash();
	foreach $x (@FieldNames) {
		print "$x: $Data{$x}\n";
	}
	print "\n";
	$count++;
}
$db->Close();
print "</pre></body></html>\n";

YnsWBRέקƮwAunΤP SQL RONiHFAаѾ\ SQL `C

beJ SQL ROɡArȭnγ޸AҦpG

$sql = "select * from table1 where NAME = 'Roger'"; pGOƭANݭnϥγ޸AҦpG $sql = "select * from table1 where AGE = 38"; pGOrAӥB٥]t޸ANnϥΨӳ޸AҦpG $sql = "select * from table1 where NAME = 'Roger''s fish'"; bWҤAȬOuRoger's fishvAN޸Ƥ@ANoWz SQL ROC

t@ӱ`I쪺DOGpϥΡu|vȡH]u|vb SQL ̭SNqA]UO@ӿ~gkG

$sql = "select * from table1 where NAME = 'P|Q'"; TgkAhOϥΡu|v ASCII XAN 'P|Q' ഫ 'P' & chr(124) & 'Q'AҥHTgkpUG $sql = "select * from table1 where NAME = 'P' & chr(124) & 'Q'";
Perl