返回列表 回复 发帖

关于odbc的一个文挡说明

作者:shenpine
email: shenpine@163.net
日期:2000-07-14 19:57:08

Win32::ODBC - Object
--------------------------------------------------------------------------------
Creating an ODBC Object
Your script will need to have the following line:
use Win32::ODBC;
        调用模块
Then you will need to create a data connection to your DSN:
$Data = new Win32::ODBC("MyDSN");
数据源的连接
You shoud check to see if $Data is indeed defined otherwise there has been an error. You can now send SQL queries and retrieve info to your heart';s content! See the description of functions below and also test.pl to see how it all works.
Make sure that you close your connection when you are finished:
$Data->Close();
关闭连接
--------------------------------------------------------------------------------
Object Methods
General Note
All methods assume that you have the line:
use Win32::ODBC;
somewhere before the method calls, and that you have an ODBC object called $db which was created using some call similar to:
$db = new Win32::ODBC("MyDSN");
See new for more information.
Also, in an effort to keep the examples short, no error checking is done on return values for any calls other than the one being exemplified. You should always check for error conditions in production code.
WARNING: The example code has not yet been tested. This will be fixed ASAP, but be forwarned!

Catalog   qualifier, owner, name, type
Retrieves the catalog from the current ODBC object.
Returns a four-element array (Qualifier, Owner, Name, Type).
Note:All fieldnames are uppercase!
Example:
($qualifier, $owner, $name, $type) = $db->Catalog("", "", "%", "';TABLE';");
从库中检索数据到下面四个变量中(Qualifier, Owner, Name, Type)

Connection
Returns the object';s ODBC connection number.
Example:
$cnum = $db->Connection;
建立连接
Close
Closes the ODBC connection for this object. It always returns undef.
Example:$db->Close();
断开连接

Data
Data list
Retrieve data from previous fetch for a list of field names.
In a scalar context it returns all of the field values concatenated together.
In an array context, it returns an array of the values, in the order in which they were
specified.
If no field names are given, all fields are returned in an unspecified order.
Example:
$db->Sql("SELECT f1, f2, f3 FROM foo");
$db->FetchRow();
($f1, $f2) = $db->Data("f1", "f2");
or
$db->Sql("SELECT * FROM foo");
$db->FetchRow();
@values = $db->Data;
See also: DataHash

DataHash
DataHash list
Retrieve data from previous fetch for a list of field names. Returns a hash where the field name is the key. If no field names are given, all fields are returned.
Example:
$db->Sql("SELECT f1, f2, f3 FROM foo");
$db->FetchRow();
%hash = $db->DataHash("f1", "f2");
print $hash{f1};
or
$db->Sql("SELECT * FROM foo");
$db->FetchRow();
%hash = $db->DataHash;
foreach $key (sort(keys %hash)) {
print $key, ';=';, $hash{$key}, "\n";
}
See also: Data

DataSources
Returns an associative array of Data Sources and ODBC remarks in the form of:
$ArrayName{';DSN';} = Remark
where DSN is the Data Source Name and Remark is, well, the remark.
Example:
%rem = $db->DataSources;
print LOG qq(Current DSN';s Remark: "), %rem{$db->GetDSN}, qq("\n);
Back to the top  
Drivers
Returns an associative array of Drivers and their attributes in the form of:
$ArrayName{';DRIVER';} = Attrib1;Attrib2;Attrib3;...
where DRIVER is the ODBC Driver Name and AttribX are the driver-defined attributes.
Example:
%attrib = $db->Drivers;
print LOG qq($driver: $attrib{$driver}\n) foreach $driver (keys %attrib);
Back to the top  
DumpError
Dump to the screen details about the last error condition. This includes error number, error text and the ODBC connection number that caused the error (if there is one). This is used primarily for debugging.
Example:
$db = new Win32::ODBC("My DSN");
if (undef $db){
    Win32::ODBC:umpError();
}
if ($db->Sql("Select * FROM foo")){
    $db->DumpError;
}
Back to the top  
DumpData
Dump to the screen all field names and the data in all rows of the current dataset. This is used primarily for debugging.
Example:
$db->Sql("Select * FROM foo");
$db->DumpData;
Back to the top  
Error
Returns the last recorded error in the form of an array or string (depending upon the context) containing the error number, error text and the ODBC connection that caused the error (if there is one).
Example:
die $db->Error(), qq(\n);
($ErrNum, $ErrText, $ErrConn) = $db->Error();
Back to the top  
FetchRow
Fetches the next row of data from the previous specified SQL statement. You would then call Data or DataHash to actually retrieve the individual elements of data. Returns undef if there';s an error, TRUE otherwise.
Example:
$db->Sql("SELECT * FROM foo");
$db->FetchRow() || die qq(Fetch error: ), $db->Error(), qq(\n);
$f1 = $db->Data("f1");
See also: Sql, Data, DataHash
Back to the top  
FieldNames
Returns a list of field names extracted from the current dataset. This is used mostly for testing/debugging. FieldNames returns the data in an array, with no guarantee of the order of the names.
Example:
$db->Sql("SELECT * FROM foo");
$db->FetchRow();
foreach $fd ($db->FieldNames()) print qq($fd: "), $db->Data($fd), qq("\n);
Back to the top  
GetConnections
Returns an array of connection numbers for all objects.
Example:
@cnums = $db->GetConnections;
Back to the top  
GetDSN
GetDSN conn
Returns the DSN (Data Source Name) or the ODBCDriverConnect string for the connection conn, or the current connection if not specified.
Example:
print LOG qq(Current connection: "), $db->GetDSN, qq("\n);
Back to the top  
GetMaxBufSize
Returns the current maximum single field data size, in bytes.
Example:
$max = $db->GetMaxBufSize;
&#36;db->SetMaxBufSize(&#36;needed) if (&#36;max < &#36;needed);
See also: SetMaxBufSize
Back to the top  
GetStmtCloseType
Returns the current ODBC close type setting. This is used mainly for debugging. Type will be one of: SQL_CLOSE, SQL_DROP, SQL_UNBIND, or SQL_RESET_PARAMS. See SetStmtCloseType for more info on what each of the types mean, and how they are used.
Example:
&#36;oldct = &#36;db->GetStmtCloseType;
&#36;db->SetStmtCloseType(SQL_DROP);
...
&#36;db->SetStmtCloseType(&#36;oldct);
See also: SetStmtCloseType
Back to the top  
MoreResults
Sees if more result sets are present and initializes for fetching rows from next result set. You would then call FetchRow to actually fetch the next row of the next result set. Returns undef if there';s an error, TRUE otherwise.
Example:
&#36;db->Sql("SELECT * FROM foo\n  SELECT * FROM bar");
&#36;db->FetchRow() || die qq(Fetch error: ), &#36;db->Error(), qq(\n);
&#36;f1 = &#36;db->Data("f1");
&#36;db->MoreResults() || die qq(Error checking for more result sets: ), &#36;db->Error(), qq(\n);
&#36;db->FetchRow() || die qq(Fetch error: ), &#36;db->Error(), qq(\n);
&#36;f1 = &#36;db->Data("f1");
See also: Sql, Data
Back to the top  
new Win32::ODBC(DSN)
new Win32::ODBC(ODBCDriverConnect)
Creates a new ODBC object, given a DSN (Data Source Name) or a properly formatted ODBCDriverConnect string. Returns the created ODBC object or undef if there is an error.
Example:
&#36;DSN = "MyDSN";
&#36;db = new Win32::ODBC(&#36;DSN);
die qq(Cannot open new ODBC\n) if ! &#36;db;
or
&#36;db = new Win32::ODBC("dsn=FOO;UID=BARWD=FUBAR");
die qq(Cannot open new ODBC\n) if ! &#36;db;
Back to the top  
RowCount
Returns the number of rows that were affected by the previous SQL command. Note: This does not work on all ODBC connections.
Example:
&#36;db->Sql("SELECT * FROM foo");
print DBG q(&#35; of records: ), &#36;db->RowCount(), qq(\n);
Back to the top  
Run
stmt
Submit the SQL statement stmt and print data about it. This is used only in debugging.
Example:
&#36;db->Run("SELECT * FROM foo");
See also: Sql
Back to the top  
SetMaxBufSize
size
Sets the maximum buffer size that a single field can allocate when executing a FetchRow. The default limit is 10240 bytes and the absolute maximum is set to 2147483647 bytes. This absolute maximum can be reset by recompiling the module. Returns undef if successful.
Example:
&#36;newsize = 20480;
&#36;rc = &#36;db->SetMaxBufSize(&#36;newsize);
die qq(SetMaxBufSize(&#36;newsize) error: ), &#36;db->Error, qq(\n) if ! &#36;rc;
See also: GetMaxBufSize
Back to the top  
SetStmtCloseType
type
Sets the current ODBC close type setting used by the ODBC Manager. This is used mainly for debugging. Normally, when you open a statement handle and perform a query (or whatever) the results are associated with the statement. You need to free the statement in order to execute another query. When you do this, usually the dataset (from the query) is cached. This caching action may be good for speed but could cause some memory problems if your dataset is huge. See the ODBC API call SQLFreeStmt(hstmt, option) for more details. (All of this is handled automatically by the Win32::ODBC package).
Type will be one of:
SQL_CLOSE - just close the statement (use caching)
SQL_DROP - close and drop all results (do not use caching)
SQL_UNBIND - close and remove bindings to columns (odbc.pll does not bind vars to columns)
SQL_RESET_PARAMS - close and reset all of the bound parameters (such as type casting for columns; see SQLFreeStmt())
Example:
&#36;oldct = &#36;db->GetStmtCloseType;
&#36;db->SetStmtCloseType(SQL_DROP);
...
&#36;db->SetStmtCloseType(&#36;oldct);
See also: GetStmtCloseType
Back to the top  
ShutDown
Closes the ODBC connection and print data about it. This is used only in debugging.
Example:
&#36;db->Shutdown;
See also: Close
Back to the top  
Sql
stmt
Executes the SQL command stmt. Returns undef on success, SQL error code on failure.
Example:
&#36;stmt = "SELECT * FROM foo";
&#36;rc = &#36;db->Sql(&#36;stmt);
die qq(SQL failed "&#36;stmt": ), &#36;db->Error(), qq(\n) if &#36;rc;
See also: Error
Back to the top  
TableList
TableList qualifier, owner, name, type
Retrieves the list of table names from the current ODBC object using Catalog. If not specified, qualifier and owner default to "", name defaults to "%", and type defaults to "';TABLE';". TableList returns an array of table names. Note:All fieldnames are uppercase!
Example:
@tables = &#36;db->TableList;
See also: Catalog


——————以下内容由Cnangel2005年08月25日 07:32pm时添加———————

    个人觉得,这篇文章并没有说明什么,并且不是作者原创,且作者只是添加了几个汉字而已,既然要说明,就要用简捷而易懂的文字来描述,不是从别处下载一个模块,把其内容贴在上面完事,本来CPAN上就有模块,且模块里有最详细的说明,何必多此一举呢!
    我把这个贴在这是因为目前我没有办法找到关于Win32的ODBC的知识(本人不在连上互连网),算做一种学习吧!

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表