iodbc - Perl extension for the iODBC API
This man page is not intended to be a manual on the ODBC API. Please see an ODBC manual for extended documentation on the usage of ODBC commands.
To use iodbc functions:
use iodbc;
Everything in the extension follows the ODBC core API except that all function calls are pass by value. The one exception is SQLBindCol which needs to be passed a reference to a scalar. For More information please seek a ODBC manual and remember that this extension only works with core implementations.
The following functions are included with this iodbc extension. Parameters marked [i] are for input into the function while parameters marked [r] return data from a function.
SQLAllocEnv
SQLAllocEnv($hEnv);
$hEnv
[o] The environment handle. This will be passed to other functions.
SQLAllocConnect
SQLAllocConnect($hEnv, $hDbc);
[i] The environment handle as it is returned from SQLAllocEnv().
SQLAllocEnv()
$hDbc
[o] The connection handle. This will be passed to other functions.
SQLConnect
SQLConnect($hDbc, $pvDsn, $ivfDsnSize, $pvUid, $ivfUidSize, $pvPwd, $ivfPwdSize);
[i] The connection handle as it is returned from SQLAllocConnect().
SQLAllocConnect()
$pvDsn
[i] The data source name. This should be a string.
$ivfDsnSize
[i] The size of $pvDsn. It should be SQL_NTS. This stands for a null terminated string.
SQL_NTS
$pvUid
[i] The user id. This should also be a string.
$ivfUidSize
[i] The size of $pvUid. It should also be SQL_NTS.
$pvPwd
[i] The password. Once again it should be a string.
$ivfPwdSize
[i] The size of pvPwd. Once again it should be SQL_NTS
SQLAllocStmt
SQLAllocStmt($hDbc, $hStmt);
$hStmt
[o] The statement handle. This will be passed to other functions.
SQLGetCursorName
SQLGetCursorName($hStmt, $pvCursor, $ivCursorMax, $ivCursorSize);
[i] The statement handle as it is returned from SQLAllocStmt().
SQLAllocStmt()
$pvCursor
[o] The cursor name associated with the $hStmt. This will be returned from the function as a string of maximum length $ivCursorMax.
$ivCursorMax
[i] The maximum size of $ivCursor.
$ivCursorSize
[i] The actual size of the string available for return to $pvCursor.
SQLSetCursorName
SQLSetCursorName($hStmt, $pvCursor, $ivfCursorSize);
[i] The Cursor Name to be associated with the $hStmt. This should be a string.
$ivfCursorSize
[i] The size of $pvCursor. This should be SQL_NTS.
SQLPrepare
SQLPrepare($hStmt, $pvSql, $ivfSqlSize);
$pvSql
[i] The SQL statement to be prepared. This should be a string.
$ivfSqlSize
[i] The size of $Sql. This should be SQL_NTS.
SQLExecute
SQLExecute($hStmt);
SQLExecDirect
SQLExecDirect($hStmt, $pvSql, $ivfSqlSize);
[i] The size of $pvSql. This should be SQL_NTS.
SQLRowCount
SQLRowCount($hStmt, $ivNumRows);
$ivNumCols
[o] The number of rows affected by the SQL statement just executed in $hStmt. This works for UPDATE, INSERT and DELETE statements.
UPDATE
INSERT
DELETE
SQLNumResultCols
SQLNumResultCols($hStmt, $ivNumCols);
[o] The Number of columns returned in a result set of a SQL statement in $hStmt.
SQLDescribeCol
SQLDescribeCol($hStmt, $ivCol, $pvColName, $ivColNameMax, $ivColNameSize, $fSqlType, $ivPrecision, $ivScale, $fNullable);
$ivCol
[i] The function will return a description of this column.
$pvColName
[o] A string that contains the name of column $ivCol
$ivColNameMax
[i] The maximum size of the column name to return to $pvColName
$ivColNameSize
[o] The size of the column name available to return to $pvColName
$fSqlType
[o] The type of data contained in column $ivCol.
$ivPrecision
[o] The precision of column $ivCol.
$ivScale
[o] The scale of column $ivCol.
$fNullable
[o] Returns whether column $ivCol allows null values.
SQLColAttributes
SQLColAttributes($hStmt, $ivCol, $fType, $pvAttrib, $ivAttribMax, $ivAttribSize, $ivAttrib);
[i] The function will return a attributes from column $ivCol.
$fType
[i] The type of attribute to return.
$pvAttrib
[o] The attribute. This will be returned from the function as a string.
$ivAttribMax
[i] The maximum size of $pvAttrib
$ivAttribSize
[o] The size of the attribute string available to return to $pvAttrib.
$ivAttrib
[o] The attribute. This will be returned as an integer.
SQLBindCol
SQLBindCol($hStmt, $ivCol, $fType, $svValue, $ivValueMax, $ivValueSize);
[i] The function will bind this column.
[i] The data type to bind.
$svValue
[i] A reference to a scalar that will store the data from a result set.
$ivValueMax
[i] The maximum size allowed for the scalar referenced by $svValue
$ivValueSize
[i] Size available to return to the scalar referenced by $svValue before SQLFetch() is called.
SQLFetch()
SQLFetch
SQLFetch($hStmt);
SQLFetch() returns data from each bound column to the scalar that was referenced by the $svValue parameter when the SQLBindCol() function was called.
SQLBindCol()
SQLError
SQLError($hfEnv, $hfDbc, $hfStmt, $pvSqlState, $fNativeError, $pvErrorMsg, $ivErrorMsgMax, $ivErrorMsgSize);
$hfEnv
[i] The environment handle as it is returned from SQLAllocEnv() or SQL_NULL_HENV.
$hfDbc
[i] The connection handle as it is returned from SQLAllocConnect() or SQL_NULL_HDBC.
$hfStmt
[i] The statement handle as it is returned from SQLAllocStmt() or SQL_NULL_HSTMT.
$pvSqlState
[o] This returns the SQLSTATE as a string.
$fNativeError
[o] This returns a Native Error Code.
$pvErrorMsg
[o] This returns an Error Message as a string.
$ivErrorMsgMax
[i] The maximum size of $pvErrorMsg
$ivErrorMsgSize
[o] The size of the string available to return to $pvErrorMsg
SQLFreeStmt
SQLFreeStmt($hStmt, $fOption);
$fOption
[i] The action to be taken by the function.
SQLCancel
SQLCancel($hStmt);
SQLTransact
SQLTransact($hEnv, $hDbc, $fType);
[i] The connection handle as it is returned from SQLAllocConnect.
[i] The type of transaction to take
SQLDisconnect
SQLDisconnect($hDbc);
SQLFreeConnect
SQLFreeConnect($hDbc);
SQLFreeEnv
SQLFreeEnv($hEnv);
[i] The connection handle as it is returned from SQLAllocEnv().
#
#To start I allocate handles and connect to my favorite data source. Notice I check every return code.
checkretcode(&SQLAllocEnv($henv));
checkretcode(SQLAllocConnect($henv, $hdbc));
checkretcode(SQLConnect($hdbc, "favorite_datsource", SQL_NTS, "user", SQL_NTS, "password", SQL_NTS));
checkretcode(SQLAllocStmt($hdbc,$hstmt));
#Then I execute a simple SQL statement
checkretcode(SQLExecDirect($hstmt, "SELECT * FROM sample_table", SQL_NTS));
#Bind column one to $rgbValue
checkretcode(SQLBindCol($hstmt, 1, SQL_C_DEFAULT, \$rgbValue, 24, SQL_NULL_DATA));
#Fetch all the rows and print them out
while(checkretcode(SQLFetch($hstmt))==SQL_SUCCESS){
print "$rgbValue\n";
}
#Finally close up shop by freeing statements and disconnecting.
checkretcode(SQLFreeStmt($hstmt, SQL_DROP));
checkretcode(SQLDisconnect($hdbc));
checkretcode(SQLFreeConnect($hdbc));
checkretcode(SQLFreeEnv($henv));
#This subroutine checks the return code to make sure that the function executed correctly
sub checkretcode {
my $retcode = shift;
if (($retcode==SQL_SUCCESS)||($retcode==SQL_NO_DATA_FOUND)) {
return $retcode;
} else {
die "some error";
Further testing. In particular add SQLCancel() and SQLTransact() to the test scripts.
SQLCancel()
SQLTransact()
Add ODBC 1.0 and then ODBC 2.0 support.
Put an DBD/DBI face on top of iODBC.
Improvement of the documentation.
Let me know if you find any.
J. Michael Mahan, mahanm@nextwork.rose-hulman.edu
perl(1).
70 POD Errors
The following errors were encountered while parsing the POD:
Expected text after =item, not a bullet
To install iodbc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm iodbc
CPAN shell
perl -MCPAN -e shell install iodbc
For more information on module installation, please visit the detailed CPAN module installation guide.