📄 ch24_01.htm
字号:
<html><head><title>ODBC Extension for Win32 (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch23_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 24. ODBC Extension for Win32</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch24_01.htm">ODBC Methods and Properties</a><br /></p></div><p><a name="INDEX-3473" /><a name="INDEX-3474" />Win32::ODBC allows you to make aconnection to a database that supports ODBC under both Win32 andUnix. Win32::ODBC allows you to support as many ODBC connections asare allowed by memory and ODBC.</p><p>Generally, you'll use Win32::ODBC to accessdatabases such as those created under Microsoft Access, butthere's also an ODBC driver available for MySQL ifyou want to connect to MySQL via ODBC and aren'table to use DBI and a DBD.</p><p>To get started, you'll need to use <tt class="literal">new()</tt> to create a new Win32::ODBC object. As arguments,<tt class="literal">new( )</tt> takes a connection string (DSN) or an openODBC object if one is given. <tt class="literal">new( )</tt> returns adatabase handle on success and <tt class="literal">undef</tt> on failure.For example:</p><blockquote><pre class="code">use Win32::ODBC;my $dsn = "DSN=Name of Database;UID=Your UID Here;PWD=dbpasswd;"my $DB = Win32::ODBC->new($dsn);</pre></blockquote><div class="sect1"><a name="perlnut2-CHP-24-SECT-1" /><h2 class="sect1">24.1. ODBC Methods and Properties</h2><p>Once you have created an ODBC object, you can use its methods oradjust its properties as you require. ODBC methods are implemented asyou'd expect with the Perl object syntax.</p><p>The following methods are defined by Win32::ODBC.</p><a name="INDEX-3475" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Catalog</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">obj</em>->Catalog(<em class="replaceable">qualifier</em>, <em class="replaceable">owner</em>, <em class="replaceable">name</em>, <em class="replaceable">type</em>)</pre><p><a name="INDEX-3475" />Tells ODBC to create a data set thatcontains table information about the DSN. Use Fetch and Data orDataHash to retrieve the data. The returned format is:</p><blockquote><pre class="code">[<em class="replaceable"><tt>qualifier</em>] [<em class="replaceable">owner</em>] [<em class="replaceable">name</em>] [<em class="replaceable">type</tt></em>]</pre></blockquote><p>Returns true on error.</p></div><a name="INDEX-3476" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ColAttributes</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->ColAttributes(<em class="replaceable">attribute</em>s, [<em class="replaceable">field_names</em>])</pre><p><a name="INDEX-3476" />Returns the attributes<em class="replaceable"><tt>attributes</tt></em> on each of the fields in thelist <em class="replaceable"><tt>field_names</tt></em> in the current recordset. If <em class="replaceable"><tt>field_names</tt></em> is empty, then allfields are assumed. The attributes are returned as an associativearray.</p></div><a name="INDEX-3477" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ConfigDSN</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->ConfigDSN(<em class="replaceable">option</em>, <em class="replaceable">driver</em>, <em class="replaceable">attribute1</em> [, <em class="replaceable">attribute2</em>, <em class="replaceable">attribute3</em>, ... ])</pre><p><a name="INDEX-3477" />Configures a DSN.<em class="replaceable"><tt>option</tt></em> takes on one of the followingvalues:</p><dl><dt><i> <tt class="literal">ODBC_ADD_DSN</tt> </i></dt><dd>Adds a new DSN</p></dd><dt><i> <tt class="literal">ODBC_MODIFY_DSN</tt> </i></dt><dd>Modifies an existing DSN</p></dd><dt><i> <tt class="literal">ODBC_REMOVE_DSN</tt> </i></dt><dd>Removes an existing DSN</p></dd><dt><i> <tt class="literal">ODBC_ADD_SYS_DSN</tt> </i></dt><dd>Adds a new System DSN</p></dd><dt><i> <tt class="literal">ODBC_MODIFY_SYS_DSN</tt> </i></dt><dd>Modifies an existing System DSN</p></dd><dt><i> <tt class="literal">ODBC_REMOVE_SYS_DSN</tt> </i></dt><dd>Removes an existing System DSN</p></dd></dl><p><em class="replaceable"><tt>driver</tt></em> is a required argument.<em class="replaceable"><tt>attribute1</tt></em> is the DSN, whose format isshown earlier. <tt class="literal">ConfigDSN( )</tt> returns true onsuccess and false on failure.</p></div><a name="INDEX-3478" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Connection</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->Connection( )</pre><p><a name="INDEX-3478" />Returns the connection numberassociated with the ODBC connection.</p></div><a name="INDEX-3479" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Close</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->Close( )</pre><p><a name="INDEX-3479" />Closesthe ODBC connection. No return value.</p></div><a name="INDEX-3480" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Data</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->Data([<em class="replaceable">field_name</em>])</pre><p><a name="INDEX-3480" />Returns the contents of columnname <em class="replaceable"><tt>field_name</tt></em> or the current row (ifnothing is specified).</p></div><a name="INDEX-3481" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>DataHash</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->DataHash([<em class="replaceable">field1</em>, <em class="replaceable">field2</em>, ...])</pre><p><a name="INDEX-3481" />Returns the contents for<em class="replaceable"><tt>field1</em>, <em class="replaceable">field2</tt></em>,<tt class="literal">...</tt>, or the entire row (if nothing is specified)as an associative array consisting of:</p><blockquote><pre class="code">{Field Name} => Field Data</pre></blockquote></div><a name="INDEX-3482" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>DataSources</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Win32::ODBC->DataSources( )</pre><p><a name="INDEX-3482" />Returns an associative array of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -