欢迎光临
我们一直在努力

关于odbc的一个文挡说明,奉献给大家-CGI教程,CGI文档

建站超值云服务器,限时71元/月

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 hearts 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 objects 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 dsns 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::dumperror();
}
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 theres 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;
$db->setmaxbufsize($needed) if ($max < $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:
$oldct = $db->getstmtclosetype;
$db->setstmtclosetype(sql_drop);

$db->setstmtclosetype($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 theres an error, true otherwise.
example:
$db->sql("select * from foo\n  select * from bar");
$db->fetchrow() || die qq(fetch error: ), $db->error(), qq(\n);
$f1 = $db->data("f1");
$db->moreresults() || die qq(error checking for more result sets: ), $db->error(), qq(\n);
$db->fetchrow() || die qq(fetch error: ), $db->error(), qq(\n);
$f1 = $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:
$dsn = "mydsn";
$db = new win32::odbc($dsn);
die qq(cannot open new odbc\n) if ! $db;

or

$db = new win32::odbc("dsn=foo;uid=bar;pwd=fubar");
die qq(cannot open new odbc\n) if ! $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:
$db->sql("select * from foo");
print dbg q(# of records: ), $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:
$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:
$newsize = 20480;
$rc = $db->setmaxbufsize($newsize);
die qq(setmaxbufsize($newsize) error: ), $db->error, qq(\n) if ! $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:
$oldct = $db->getstmtclosetype;
$db->setstmtclosetype(sql_drop);

$db->setstmtclosetype($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:
$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:
$stmt = "select * from foo";
$rc = $db->sql($stmt);
die qq(sql failed "$stmt": ), $db->error(), qq(\n) if $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 = $db->tablelist;
see also: catalog

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 关于odbc的一个文挡说明,奉献给大家-CGI教程,CGI文档
分享到: 更多 (0)