The Win32::ODBC Module

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

Win32::ODBC, written by Dave Roth, is module extension to Win32 Perl. Dave Roth has created a wonderful set of methods that make connecting to you( win32 database a portable and simple task. Using win32::ODBC methods, you can connect to your database, create tables, insert data, and retrieve data. Here you will learn about the more frequently used methods of win32::ODBC.

The Close Method

The close method disconnects your program from the database engine. You should always close your database connection. Do not depend on Perl to close the open file handle. Your database engine is likely to have opened working files, which may not be properly closed when your program exits.

The close method requries an object reference as its first and only parameter

$myDb >close(); The Data Method

The data method accesses a data structure internal to the win32::ODBC module to retrieve the column values of individual row. The Data method has meaning only after your program has retrieved data from your database through a Sol statement. The Data method retrieves the columns from the current row, use the FetchRow method to get the row of data.

You can retrieve all the columns of row in unspecified order like this:

lvalue = $myDb >Data();

You can retrieve an ordered list of the columns in a row by specifying which columns you wish to retrieve, as in this example: lvalue = $myDb >Data(lastName,firstName, Address),

The data will be returned in the same order as requested. If the Ivalue is a scalar, the Data method returns the column values in one concatenated string. If the Ivalue is a list, the data is returned in list context, where each item in the list is a column va lue.

The DataHash Method

The DataHash method accesses an internal data structure to retrieve the column values of an individual row. The DataHash method only has meaning after your program has retrieved data from your database through a SQL statement. The DataHash method retrieves the columns from the current row, use FetchRow method to get the next row of data.

The DataHash method returns a hash containing the column name and column values of the current row. You can retrieve specific columns by providing the columnnames, like this:

%columnData = $myDb--> Data Hash(f i rstName, lastName, Address),

FetchRow Method

The FetchRow method retrieves the next row of data from the latest SQL query of your database. The FetchRow method must be used with an object reference, which points to a valid ODBC connection:

My $myDb = win32::ODBC >new($DSN);
$myDb--> Fetch Row();
The FetchRow method populates an internal data structure. After calling the FetchRow method, you must call either the Data or DataHash method to retrieve the column values of a row. The FetchRow method returns under if there is an error or no more data to retrieve, which makes it suitable for use in a while loop, like this: while($myDb-->FetchRow(){
%model = $myDb--> Data Hash();

The New Method

The new method is the win32::ODBC class constructor. The new method takes one parameter, a valid DSN, and returns an ODBC object reference. my $myDb = win32::ODBC >new("Auto Ads"); The ODBC object reference is then used to communicate further ODBC commands to the connected database.

RowCount Method

If the new method fails, it returns undef. Always check the result returned by the new method. If an invalid result is returned your best solution is to print or log the error message and exit your program: Sometimes, you just want to count the number of rows in a table. The quickes method to accomplish this task is the RowCount method. The RowCount method returns the number of rows that were traversed by the last SQL command $myDb-->sql($SQL)
$numberOfRows=$myDb-->RowCount();

Sql Method

The Sql method is the real workhorse of the win32::ODBC class, but it really isn't an ODBC command. The Sql method makes a call to ODBCExecute, which passes the SQL statement to the database.

The Sql method returns undef on success. This means your error checking syntax is reversed. Instead of checking for a positive response to indicate success, you check for a positive response to indicate failure. In the example above, the error handling code will be called when anything other than undef is returned by the Sql method.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.