From gchiaramonte@ibl.bm Wed Feb 2 12:50:21 2000 From: gchiaramonte@ibl.bm (Gene Chiaramonte) Date: Wed, 2 Feb 2000 08:50:21 -0400 Subject: [DB-SIG] Python and Codebase from Sequiter In-Reply-To: <38959E11.9C6BFBDA@turnhere.com> Message-ID: Does anyone know if there is a Python module to access Codebase on windows? I don't have SWIG set up here, but if someone else does, I can supply you with the header file for codebase and we could give it a try. Let me know. Thanks, Gene > -----Original Message----- > From: db-sig-admin@python.org [mailto:db-sig-admin@python.org]On > Behalf Of alexander smishlajev > Sent: Monday, January 31, 2000 10:37 AM > To: db-sig@python.org > Cc: mal@lemburg.com > Subject: Re: [DB-SIG] Next Version > > > "M.-A. Lemburg" wrote: > > > > > - list of available (configured) data sources. > > > > This can only be done for database manager, e.g. the ODBC > > managers in Windows, iODBC and unixODBC. mxODBC will have > > support for this in a future version. > > such information is avalable at least for Oracle connections. for > PostgreSQL, a list of host databases may be obtained after successfull > connection. > > > > - list of database users: logon name, groups/roles > > > - list of user groups (roles): name, members > > > > Don't know where you would get these infos... you normally > > have to have admin priviledges to even look at those sys tables. > > not always. as far as i know, this information is by default available > to public on PostgreSQL, MS SQL and Oracle. of course, db admin may > reconfigure this, but at least a list of groups/roles assigned to > current login usually can be obtained as well as login name itself. > > > An abstraction layer could query the sys tables directly, BTW. > > yes, it could. but that would require some knowledge about the > implementation of such things in _each_ database. isn't it a job for db > driver? > > > > - list of database objects: owner (schema), name, type (table, > > > view, alias/synonym, procedure etc.), temporary object flag, > > > server-specific info and maybe permission list? synonyms must > > > point to corresponding object. > > > > Don't know how other DBs implement this, but mxODBC has the > > catalog methods for these things. > > as far as i know, each server has it's own way to access catalog > information. again, i think that db driver should know a way of doing > this for handled database. > > > > and... in api spec v2.0 i cannot find anything about writing > > > blobs to database. am i missing something? > > > > See the type object section: blobs should be wrapped into > > Binary(string) objects and then passed to the .executeXXX() > > methods as parameter. On output you will see a column type > > equal to the BINARY type object in the cursor description. > > i was told that this approach is not working for DCOracle. at least not > with procedure calls. perhaps allowed LOB operations should be somehow > reported by a db driver? > > best wishes, > alex. > > > _______________________________________________ > DB-SIG maillist - DB-SIG@python.org > http://www.python.org/mailman/listinfo/db-sig From gstein@lyra.org Wed Feb 2 22:53:13 2000 From: gstein@lyra.org (Greg Stein) Date: Wed, 2 Feb 2000 14:53:13 -0800 (PST) Subject: [DB-SIG] Next Version In-Reply-To: <3894AAB1.B2B965DC@lemburg.com> Message-ID: On Sun, 30 Jan 2000, M.-A. Lemburg wrote: > Stuart 'Zen' Bishop wrote: >... > > cur.longlength = 2 * 1024 * 1024 > > cur.truncok = 1 > > The truncok attribute is interesting... I would suggest to have > a .dontreportwarnings attribute (or something along those lines) > on both cursor and connection objects (or some other way to turn off > reporting of warnings via exceptions). There are sometimes troubles > with ODBC functions returning warnings and mxODBC having to > raise a Warning exception: since some sequences cannot be completed > because of the warning exceptions, there is considerable loss > of functionality results for some DB backends (MS SQL Server being > a prominent example). mxODBC currently solves this by simply ignoring > some warnings... Note that the setoutputsize() method is DBAPI's mechanism for handling this. Error handling when a truncation occurs is database-dependent. Cheers, -g -- Greg Stein, http://www.lyra.org/ From mal@lemburg.com Wed Feb 2 23:37:25 2000 From: mal@lemburg.com (M.-A. Lemburg) Date: Thu, 03 Feb 2000 00:37:25 +0100 Subject: [DB-SIG] Next Version References: Message-ID: <3898BFB5.65802F1D@lemburg.com> Greg Stein wrote: > > On Sun, 30 Jan 2000, M.-A. Lemburg wrote: > > Stuart 'Zen' Bishop wrote: > >... > > > cur.truncok = 1 > > > > The truncok attribute is interesting... I would suggest to have > > a .dontreportwarnings attribute (or something along those lines) > > on both cursor and connection objects (or some other way to turn off > > reporting of warnings via exceptions). There are sometimes troubles > > with ODBC functions returning warnings and mxODBC having to > > raise a Warning exception: since some sequences cannot be completed > > because of the warning exceptions, there is considerable loss > > of functionality results for some DB backends (MS SQL Server being > > a prominent example). mxODBC currently solves this by simply ignoring > > some warnings... > > Error handling when a truncation occurs is database-dependent. Truncation yes, but there are also other warnings we limit usage in some cases. -- Marc-Andre Lemburg ______________________________________________________________________ Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/ Return-Path: Delivered-To: db-sig@dinsdale.python.org Received: from python.org (parrot.python.org [132.151.1.90]) by dinsdale.python.org (Postfix) with ESMTP id 4F46B1CD20 for ; Sat, 29 Jan 2000 20:24:05 -0500 (EST) Received: from wombat.cs.rmit.edu.au (wombat.cs.rmit.edu.au [131.170.24.41]) by python.org (8.9.1a/8.9.1) with ESMTP id UAA00174 for ; Sat, 29 Jan 2000 20:23:53 -0500 (EST) Received: from goanna.cs.rmit.edu.au (zen@goanna.cs.rmit.edu.au [131.170.24.40]) by wombat.cs.rmit.edu.au (8.9.3/8.9.3/cshub) with ESMTP id MAA15313 for ; Sun, 30 Jan 2000 12:25:13 +1100 (EST) Received: (from zen@localhost) by goanna.cs.rmit.edu.au (8.9.3/8.9.3/csnode) id MAA04270; Sun, 30 Jan 2000 12:25:12 +1100 (EST) Date: Sun, 30 Jan 2000 12:25:12 +1100 (EST) From: "Stuart 'Zen' Bishop" Reply-To: zen@cs.rmit.edu.au To: db-sig@python.org Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: [DB-SIG] PerlDBI docs Sender: db-sig-admin@python.org Errors-To: db-sig-admin@python.org X-BeenThere: db-sig@python.org X-Mailman-Version: 1.2 (experimental) Precedence: bulk List-Id: SIG on Python Tabular Databases Below is a copy of the PerDBI documentation which was supposed to be attached to my last email :-) User Contributed Perl Documentation DBI(3) NAME DBI - Database independent interface for Perl SYNOPSIS use DBI; @driver_names = DBI->available_drivers; @data_sources = DBI->data_sources($driver_name); $dbh = DBI->connect($data_source, $username, $auth); $dbh = DBI->connect($data_source, $username, $auth, \%attr); $rv = $dbh->do($statement); $rv = $dbh->do($statement, \%attr); $rv = $dbh->do($statement, \%attr, @bind_values); @row_ary = $dbh->selectrow_array($statement); $ary_ref = $dbh->selectall_arrayref($statement); $sth = $dbh->prepare($statement); $sth = $dbh->prepare_cached($statement); $rv = $sth->bind_param($p_num, $bind_value); $rv = $sth->bind_param($p_num, $bind_value, $bind_type); $rv = $sth->bind_param($p_num, $bind_value, \%attr); $rv = $sth->execute; $rv = $sth->execute(@bind_values); $rc = $sth->bind_col($col_num, \$col_variable); $rc = $sth->bind_columns(\%attr, @list_of_refs_to_vars_to_bind); @row_ary = $sth->fetchrow_array; $ary_ref = $sth->fetchrow_arrayref; $hash_ref = $sth->fetchrow_hashref; $ary_ref = $sth->fetchall_arrayref; $rv = $sth->rows; $rc = $dbh->commit; $rc = $dbh->rollback; $sql = $dbh->quote($string); $rc = $h->err; $str = $h->errstr; $rv = $h->state; $rc = $dbh->disconnect; 22/Jul/99 Last change: perl 5.005, patch 02 1 User Contributed Perl Documentation DBI(3) NOTE This is the DBI specification that corresponds to the DBI version 1.06 ($Date: 1999/01/15 05:33:37 $). The DBI specification is currently evolving quite quickly so it is important to check that you have the latest copy. The RECENT CHANGES section below has a summary of user-visible changes and the Changes file supplied with the DBI holds more detailed change information. Note also that whenever the DBI changes the drivers take some time to catch up. Recent versions of the DBI have added many new features (marked *NEW* in the text) that may not yet be supported by the drivers you use. Talk to the authors of those drivers if you need the features. Please also read the DBI FAQ which is installed as a DBI::FAQ module so you can use perldoc to read it by executing the perldoc DBI::FAQ command. RECENT CHANGES A brief summary of significant user-visible changes in recent versions (if a recent version isn't mentioned it simply means that there were no significant user-visible changes in that version). DBI 1.00 - 14th August 1998 Added $dbh->table_info. DBI 0.96 - 10th August 1998 Added $sth->{PRECISION} and $sth->{SCALE}. Added DBD::Shell and dbish interactive DBI shell. Any database attribs can be set via DBI->connect(,,, \%attr). Added _get_fbav and _set_fbav methods for Perl driver developers. DBI trace now shows appends " at yourfile.pl line nnn". PrintError and RaiseError now prepend driver and method name. Added $dbh->{Name}. Added $dbh->quote($value, $data_type). Added DBD::Proxy and DBI::ProxyServer (from Jochen Wiedmann). Added $dbh->selectall_arrayref and $dbh->selectrow_array methods. Added $dbh->table_info. Added $dbh->type_info and $dbh->type_info_all. Added $h->trace_msg($msg) to write to trace log. Added @bool = DBI::looks_like_number(@ary). DBI 0.92 - 4th February 1998 Added $dbh->prepare_cached() caching variant of $dbh->prepare. Added new attributes: Active, Kids, ActiveKids, CachedKids. Added support for general- purpose 'private_' attributes. 22/Jul/99 Last change: perl 5.005, patch 02 2 User Contributed Perl Documentation DBI(3) DESCRIPTION The Perl DBI is a database access Application Programming Interface (API) for the Perl Language. The DBI defines a set of functions, variables and conventions that provide a consistent database interface independant of the actual database being used. It is important to remember that the DBI is just an interface. A thin layer of 'glue' between an application and one or more Database Drivers. It is the drivers which do the real work. The DBI provides a standard interface and framework for the drivers to operate within. Architecture of a DBI Application |<- Scope of DBI ->| .-. .--------------. .-------------. .-------. | |---| XYZ Driver |---| XYZ Engine | | Perl | |S| `--------------' `-------------' | script| |A| |w| .--------------. .-------------. | using |--|P|--|i|---|Oracle Driver |---|Oracle Engine| | DBI | |I| |t| `--------------' `-------------' | API | |c|... |methods| |h|... Other drivers `-------' | |... `-' The API is the Application Perl-script (or Programming) Interface. The call interface and variables provided by DBI to perl scripts. The API is implemented by the DBI Perl extension. The 'Switch' is the code that 'dispatches' the DBI method calls to the appropriate Driver for actual execution. The Switch is also responsible for the dynamic loading of Drivers, error checking/handling and other duties. The DBI and Switch are generally synonymous. The Drivers implement support for a given type of Engine (database). Drivers contain implementations of the DBI methods written using the private interface functions of the corresponding Engine. Only authors of sophisticated/multi- database applications or generic library functions need be concerned with Drivers. Notation and Conventions 22/Jul/99 Last change: perl 5.005, patch 02 3 User Contributed Perl Documentation DBI(3) DBI static 'top-level' class name $dbh Database handle object $sth Statement handle object $drh Driver handle object (rarely seen or used in applications) $h Any of the $??h handle types above $rc General Return Code (boolean: true=ok, false=error) $rv General Return Value (typically an integer) @ary List of values returned from the database, typically a row of data $rows Number of rows processed (if available, else -1) $fh A filehandle undef NULL values are represented by undefined values in perl \%attr Reference to a hash of attribute values passed to methods Note that Perl will automatically destroy database and statement objects if all references to them are deleted. Handle object attributes are shown as: $h->{attribute_name} (type) where type indicates the type of the value of the attribute (if it's not a simple scalar): \$ reference to a scalar: $h->{attr} or $a = ${$h->{attr}} \@ reference to a list: $h->{attr}->[0] or @a = @{$h->{attr}} \% reference to a hash: $h->{attr}->{a} or %a = %{$h->{attr}} General Interface Rules & Caveats The DBI does not have a concept of a `current session'. Every session has a handle object (i.e., a $dbh) returned from the connect method and that handle object is used to invoke database related methods. Most data is returned to the perl script as strings (null values are returned as undef). This allows arbitrary precision numeric data to be handled without loss of accuracy. Be aware that perl may not preserve the same accuracy when the string is used as a number. Dates and times are returned as character strings in the native format of the corresponding Engine. Time Zone effects are Engine/Driver dependent. Perl supports binary data in perl strings and the DBI will pass binary data to and from the Driver without change. It is up to the Driver implementors to decide how they wish to handle such binary data. Multiple SQL statements may not be combined in a single statement handle, e.g., a single $sth. 22/Jul/99 Last change: perl 5.005, patch 02 4 User Contributed Perl Documentation DBI(3) Non-sequential record reads are not supported in this version of the DBI. E.g., records can only be fetched in the order that the database returned them and once fetched they are forgotten. Positioned updates and deletes are not directly supported by the DBI. See the description of the CursorName attribute for an alternative. Individual Driver implementors are free to provide any private functions and/or handle attributes that they feel are useful. Private driver functions can be invoked using the DBI func method. Private driver attributes are accessed just like standard attributes. Many methods have an optional \%attr parameter which can be used to pass information to the driver implementing the method. Except where specifically documented the \%attr parameter can only be used to pass driver specific hints. In general you can ignore \%attr parameters or pass it as undef. Character sets: Most databases which understand character sets have a default global charset and text stored in the database is, or should be, stored in that charset (if it's not then that's the fault of either the database or the application that inserted the data). When text is fetched it should be (automatically) converted to the charset of the client (presumably based on the locale). If a driver needs to set a flag to get that behaviour then it should do so. It should not require the application to do that. Naming Conventions and Name Space The DBI package and all packages below it (DBI::*) are reserved for use by the DBI. Package names beginning with DBD:: are reserved for use by DBI database drivers. All environment variables used by the DBI or DBD's begin with 'DBI_' or 'DBD_'. The letter case used for attribute names is significant and plays an important part in the portability of DBI scripts. The case of the attribute name is used to signify who defined the meaning of that name and its values. Case of name Has a meaning defined by ------------ ------------------------ UPPER_CASE Standards, e.g., X/Open, SQL92 etc (portable) MixedCase DBI API (portable), underscores are not used. lower_case Driver or Engine specific (non-portable) It is of the utmost importance that Driver developers only 22/Jul/99 Last change: perl 5.005, patch 02 5 User Contributed Perl Documentation DBI(3) use lowercase attribute names when defining private attributes. Private attribute names must be prefixed with the driver name or suitable abbreviation (e.g., ora_ for Oracle, ing_ for Ingres etc). Driver Specific Prefix Registry: ora_ DBD::Oracle ing_ DBD::Ingres odbc_ DBD::ODBC syb_ DBD::Sybase db2_ DBD::DB2 ix_ DBD::Informix csv_ DBD::CSV file_ DBD::TextFile xbase_ DBD::XBase solid_ DBD::Solid proxy_ DBD::Proxy Outline Usage First you need to load the DBI module: use DBI; (also adding use strict; is recommended) then the connect entry elsewhere in this documentto your data source: $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 }); The DBI allows an application to `prepare' statements for later execution. A prepared statement is identified by a statement handle object, e.g., $sth. Typical method call sequence for a select statement: prepare, execute, fetch, fetch, ... execute, fetch, fetch, ... execute, fetch, fetch, ... for example: $sth = $dbh->prepare("select foo, bar from table where baz=?"); $sth->execute($baz); while(@row = $sth->fetchrow_array) { print "@row\n"; } 22/Jul/99 Last change: perl 5.005, patch 02 6 User Contributed Perl Documentation DBI(3) Typical method call sequence for a non-select statement: prepare, execute, execute, execute. for example: $sth = $dbh->prepare("insert into table(foo,bar,baz) values (?,?,?)"); while() { chop; my ($foo,$bar,$baz) = split /,/; $sth->execute($foo,$bar,$baz); } To commit your changes to the database (when the AutoCommit entry elsewhere in this documentis off): $dbh->commit; # or call $dbh->rollback; to undo changes Finally, when you have finished working with the data source you should the disconnect entry elsewhere in this documentfrom it: $dbh->disconnect; Placeholders and Bind Values Some drivers support Placeholders and Bind Values. These drivers allow a database statement to contain placeholders, sometimes called parameter markers, that indicate values that will be supplied later, before the prepared statement is executed. For example, an application might use the following to insert a row of data into the SALES table: insert into sales (product_code, qty, price) values (?, ?, ?) or the following, to select the description for a product: select description from products where product_code = ? The ? characters are the placeholders. The association of actual values with placeholders is known as binding and the values are referred to as bind values. When using placeholders with the SQL LIKE qualifier you must remember that the placeholder substitutes for the whole string. So you should use "... LIKE ? ..." and include any wildcard characters in the value that you bind to the 22/Jul/99 Last change: perl 5.005, patch 02 7 User Contributed Perl Documentation DBI(3) placeholder. Null Values Undefined values or undef can be used to indicate null values. However, care must be taken in the particular case of trying to use null values to qualify a select statement. Consider: select description from products where product_code = ? Binding an undef (NULL) to the placeholder will not select rows which have a NULL product_code! Refer to the SQL manual for your database engine or any SQL book for the reasons for this. To explicitly select NULLs you have to say "where product_code is NULL" and to make that general you have to say: ... where (product_code = ? or (? is null and product_code is null)) and bind the same value to both placeholders. Performance Without using placeholders, the insert statement above would have to contain the literal values to be inserted and it would have to be re-prepared and re-executed for each row. With placeholders, the insert statement only needs to be prepared once. The bind values for each row can be given to the execute method each time it's called. By avoiding the need to re-prepare the statement for each row the application typically many times faster! Here's an example: my $sth = $dbh->prepare(q{ insert into sales (product_code, qty, price) values (?, ?, ?) }) || die $dbh->errstr; while (<>) { chop; my ($product_code, $qty, $price) = split /,/; $sth->execute($product_code, $qty, $price) || die $dbh->errstr; } $dbh->commit || die $dbh->errstr; See the execute and bind_param entries elsewhere in this documentfor more details. The q{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like qq{...} operator if you want to interpolate variables into the string. See the section on Quote and Quote-like Operators in the perlop manpage for more details. 22/Jul/99 Last change: perl 5.005, patch 02 8 User Contributed Perl Documentation DBI(3) See the bind_column entry elsewhere in this documentfor a related method used to associate perl variables with the output columns of a select statement. SQL - A Query Language Most DBI drivers require applications to use a dialect of SQL (the Structured Query Language) to interact with the database engine. These links provide useful information and further links about SQL: http://w3.one.net/~jhoffman/sqltut.htm http://www.jcc.com/sql_stnd.html http://www.contrib.andrew.cmu.edu/~shadow/sql.html The DBI itself does not mandate or require any particular language to be used. It is language independant. In ODBC terms the DBI is in 'pass-thru' mode (individual drivers might not be). The only requirement is that queries and other statements must be expressed as a single string of letters passed as the first argument to the the prepare entry elsewhere in this documentmethod. THE DBI CLASS DBI Class Methods connect $dbh = DBI->connect($data_source, $username, $password) || die $DBI::errstr; $dbh = DBI->connect($data_source, $username, $password, \%attr) || die $DBI::errstr; Establishes a database connection (session) to the requested data_source. Returns a database handle object if the connect succeeds. Use $dbh->disconnect to terminate the connection. If the connect fails (see below) it returns undef and sets $DBI::err and $DBI::errstr (it does not set $! etc). Generally you should test the return status of connect and print $DBI::errstr if it has failed. Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one connect call for each and keep a copy of each returned database handle. The $data_source value should begin with 'dbi:driver_name:'. That prefix will be stripped off and the driver_name part is used to specify the driver (letter case is significant). As a convenience, if the $data_source field is undefined 22/Jul/99 Last change: perl 5.005, patch 02 9 User Contributed Perl Documentation DBI(3) or empty the DBI will substitute the value of the environment variable DBI_DSN. If the driver_name part is empty (i.e., data_source prefix is 'dbi::') the environment variable DBI_DRIVER is used. If that variable is not set then the connect dies. Examples of $data_source values: dbi:DriverName:database_name dbi:DriverName:database_name@hostname:port dbi:DriverName:database=database_name;host=hostname;port=port There is no standard for the text following the driver name. Each driver is free to use whatever syntax it wants. The only requirement the DBI makes is that all the information is supplied in a single string. You must consult the documentation for the drivers you are using for a description of the syntax they require. (Where a driver author needs to define a syntax for the data_source it is recommended that they follow the ODBC style, the last example above.) If the environment variable DBI_AUTOPROXY is defined (and the driver in $data_source is not 'Proxy') then the connect request will automatically be changed to: dbi:Proxy:$ENV{DBI_AUTOPROXY};dsn=$data_source and passed to the DBD::Proxy module. DBI_AUTOPROXY would typically be "hostname=...;port=...". See the DBD::Proxy manpage for more details. If $username or $password are undefined (rather than empty) then the DBI will substitute the values of the DBI_USER and DBI_PASS environment variables respectively. The use of the environment for these values is not recommended for security reasons. The mechanism is only intended to simplify testing. DBI->connect automatically installs the driver if it has not been installed yet. Driver installation always returns a valid driver handle or it dies with an error message which includes the string 'install_driver' and the underlying problem. So, DBI->connect will die on a driver installation failure and will only return undef on a connect failure, for which $DBI::errstr will hold the error. The $data_source argument (with the 'dbi:...:' prefix removed) and the $username and $password arguments are then passed to the driver for processing. The DBI does not define any interpretation for the contents of these 22/Jul/99 Last change: perl 5.005, patch 02 10 User Contributed Perl Documentation DBI(3) fields. The driver is free to interpret the data_source, username and password fields in any way and supply whatever defaults are appropriate for the engine being accessed (Oracle, for example, uses the ORACLE_SID and TWO_TASK env vars if no data_source is specified). The AutoCommit and PrintError attributes for each connection default to default to on (see the AutoCommit and PrintError entries elsewhere in this documentfor more information). The \%attr parameter can be used to alter the default settings of the PrintError, RaiseError, AutoCommit and other attributes. For example: $dbh = DBI->connect($data_source, $user, $pass, { PrintError => 0, AutoCommit => 0 }); Portable applications should not assume that a single driver will be able to support multiple simultaneous sessions. Where possible each session ($dbh) is independent from the transactions in other sessions. This is useful where you need to hold cursors open across transactions, e.g., use one session for your long lifespan cursors (typically read-only) and another for your short update transactions. For compatibility with old DBI scripts the driver can be specified by passing its name as the fourth argument to connect (instead of \%attr): $dbh = DBI->connect($data_source, $user, $pass, $driver); In this 'old-style' form of connect the $data_source should not start with 'dbi:driver_name:' and, even if it does, the embedded driver_name will be ignored. The $dbh->{AutoCommit} attribute is undefined. The $dbh->{PrintError} attribute is off. And the old DBI_DBNAME env var is checked if DBI_DSN is not defined. This 'old-style' connect will be withdrawn in a future version. available_drivers @ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet); Returns a list of all available drivers by searching for 22/Jul/99 Last change: perl 5.005, patch 02 11 User Contributed Perl Documentation DBI(3) DBD::* modules through the directories in @INC. By default a warning will be given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning. data_sources @ary = DBI->data_sources($driver); @ary = DBI->data_sources($driver, \%attr); Returns a list of all data sources (databases) available via the named driver. The driver will be loaded if not already. If $driver is empty or undef then the value of the DBI_DRIVER environment variable will be used. Data sources will be returned in a form suitable for passing to the the connect entry elsewhere in this documentmethod, i.e., they will include the "dbi:$driver:" prefix. Note that many drivers have no way of knowing what data sources might be available for it and thus, typically, return an empty or incomplete list. trace DBI->trace($trace_level) DBI->trace($trace_level, $trace_file) DBI trace information can be enabled for all handles using this DBI class method. To enable trace information for a specific handle use the similar $h->trace method described elsewhere. Use $trace_level 2 to see detailed call trace information including parameters and return values. The trace output is detailed and typically very useful. Much of the trace output is formatted using the the neat entry elsewhere in this documentfunction and thus strings in the trace output may be edited and truncated by it. Use $trace_level 0 to disable the trace. Initially trace output is written to STDERR. If $trace_filename is specified then the file is opened in append mode and all trace output (including that from other handles) is redirected to that file. Further calls to trace without a $trace_filename do not alter where the trace output is sent. 22/Jul/99 Last change: perl 5.005, patch 02 12 User Contributed Perl Documentation DBI(3) See also the $h->trace() method and the DEBUGGING entry elsewhere in this documentfor information about the DBI_TRACE environment variable. DBI Utility Functions neat $str = DBI::neat($value, $maxlen); Return a string containing a neat (and tidy) representation of the supplied value. Strings will be quoted (but internal quotes will not be escaped). Values known to be numeric will be unquoted. Undefined (NULL) values will be shown as undef (without quotes). Unprintable characters will be replaced by dot (.). For result strings longer than $maxlen the result string will be truncated to $maxlen-4 and ...' will be appended. If $maxlen is 0 or undef it defaults to $DBI::neat_maxlen which, in turn, defaults to 400. This function is designed to format values for human consumption. It is used internally by the DBI for the trace entry elsewhere in this documentoutput. It should typically not be used for formating values for database use (see also the quote entry elsewhere in this document). neat_list $str = DBI::neat_list(\@listref, $maxlen, $field_sep); Calls DBI::neat on each element of the list and returns a string containing the results joined with $field_sep. $field_sep defaults to ", ". looks_like_number @bool = DBI::looks_like_number(@array); Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns undef for each element that is undefined or empty. DBI Dynamic Attributes These attributes are always associated with the last handle used. 22/Jul/99 Last change: perl 5.005, patch 02 13 User Contributed Perl Documentation DBI(3) Where an attribute is equivalent to a method call, then refer to the method call for all related documentation. Warning: these attributes are provided as a convenience but they do have limitations. Specifically, because they are associated with the last handle used, they should only be used immediately after calling the method which 'sets' them. They have a 'short lifespan'. There may also be problems with the multi-threading in 5.005. If in any doubt, use the corresponding method call. $DBI::err Equivalent to $h->err. $DBI::errstr Equivalent to $h->errstr. $DBI::state Equivalent to $h->state. $DBI::rows Equivalent to $h->rows. METHODS COMMON TO ALL HANDLES err $rv = $h->err; Returns the native database engine error code from the last driver function called. errstr $str = $h->errstr; Returns the native database engine error message from the last driver function called. state $str = $h->state; Returns an error code in the standard SQLSTATE five character format. Note that the specific success code 00000 is translated to 0 (false). If the driver does not support SQLSTATE then state will return S1000 (General Error) for all errors. trace 22/Jul/99 Last change: perl 5.005, patch 02 14 User Contributed Perl Documentation DBI(3) $h->trace($trace_level); $h->trace($trace_level, $trace_filename); DBI trace information can be enabled for a specific handle (and any future children of that handle) by setting the trace level using the trace method. Use $trace_level 2 to see detailed call trace information including parameters and return values. The trace output is detailed and typically very useful. Use $trace_level 0 to disable the trace. Initially trace output is written to STDERR. If $trace_filename is specified then the file is opened in append mode and all trace output (including that from other handles) is redirected to that file. Further calls to trace without a $trace_filename do not alter where the trace output is sent. See also the DBI->trace() method and the DEBUGGING entry elsewhere in this documentfor information about the DBI_TRACE environment variable. The the neat entry elsewhere in this documentfunction is often used to format trace information and thus strings in the trace output may be edited and truncated by it. trace_msg $h->trace_msg($message_text); Writes $message_text to trace file if trace is enabled for $h or for the DBI as a whole. Can also be called as DBI->trace_msg($msg). See the trace entry elsewhere in this document. func $h->func(@func_arguments, $func_name); The func method can be used to call private non-standard and non-portable methods implemented by the driver. Note that the function name is given as the last argument. This method is not directly related to calling stored procedures. Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways. See driver documentation for more details. ATTRIBUTES COMMON TO ALL HANDLES These attributes are common to all types of DBI handles. 22/Jul/99 Last change: perl 5.005, patch 02 15 User Contributed Perl Documentation DBI(3) Some attributes are inherited by child handles. That is, the value of an inherited attribute in a newly created statement handle is the same as the value in the parent database handle. Changes to attributes in the new statement handle do not affect the parent database handle and changes to the database handle do not affect existing statement handles, only future ones. Attempting to set or get the value of an unknown attribute is fatal, except for private driver specific attributes (which all have names starting with a lowercase letter). Example: $h->{AttributeName} = ...; # set/write ... = $h->{AttributeName}; # get/read Warn (boolean, inherited) Enables useful warnings for certain bad practices. Enabled by default. Some emulation layers, especially those for perl4 interfaces, disable warnings. Active (boolean, read-only) True if the handle object is 'active'. This is rarely used in applications. The exact meaning of active is somewhat vague at the moment. For a database handle it typically means that the handle is connected to a database ($dbh->disconnect should set Active off). For a statement handle it typically means that the handle is a select that may have more data to fetch ($dbh->finish or fetching all the data should set Active off). Kids (integer, read-only) For a driver handle, Kids is the number of currently existing database handles that were created from that driver handle. For a database handle, Kids is the number of currently existing statement handles that were created from that database handle. ActiveKids (integer, read-only) Like Kids (above), but only counting those that are Active (as above). CachedKids (hash ref) For a database handle, returns a reference to the cache (hash) of statement handles created by the the prepare_cached entry elsewhere in this documentmethod. For a driver handle, it would return a reference to the cache (hash) of statement handles created by the (not yet implemented) connect_cached method. 22/Jul/99 Last change: perl 5.005, patch 02 16 User Contributed Perl Documentation DBI(3) CompatMode (boolean, inherited) Used by emulation layers (such as Oraperl) to enable compatible behaviour in the underlying driver (e.g., DBD::Oracle) for this handle. Not normally set by application code. InactiveDestroy (boolean) This attribute can be used to disable the database related effect of DESTROY'ing a handle (which would normally close a prepared statement or disconnect from the database etc). It is specifically designed for use in UNIX applications which 'fork' child processes. Either the parent or the child process, but not both, should set InactiveDestroy on all their handles. For a database handle, this attribute does not disable an explicit call to the disconnect method, only the implicit call from DESTROY. PrintError (boolean, inherited) This attribute can be used to force errors to generate warnings (using warn) in addition to returning error codes in the normal way. When set on, any method which results in an error occuring will cause the DBI to effectively do a warn("$class $method failed $DBI::errstr") where $class is the driver class and $method is the name of the method which failed. E.g., DBD::Oracle::db prepare failed: ... error text here ... By default DBI->connect sets PrintError on (except for old-style connect usage, see connect for more details). If desired, the warnings can be caught and processed using a $SIG{__WARN__} handler or modules like CGI::ErrorWrap. RaiseError (boolean, inherited) This attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It defaults to off. When set on, any method which results in an error occuring will cause the DBI to effectively do a die("$class $method failed $DBI::errstr") where $class is the driver class and $method is the name of the method which failed. E.g., DBD::Oracle::db prepare failed: ... error text here ... If PrintError is also on then the PrintError is done before the RaiseError unless no __DIE__ handler has been defined, in which case PrintError is skipped since the die will print the message. 22/Jul/99 Last change: perl 5.005, patch 02 17 User Contributed Perl Documentation DBI(3) If you want to temporarily turn RaiseError off (inside a library function that is likely to fail for example), the recommended way is like this: { local $h->{RaiseError} = 0 if $h->{RaiseError}; ... } The original value will automatically and reliably be restored by perl regardless of how the block is exited. The ... if $h-{RaiseError}> is optional but makes the code slightly faster in the common case. The same logic applies to other attributes, including RaiseError. Sadly this doesn't work for perl versions upto and including 5.004_04. For backwards compatibility could just use eval { ... } instead. ChopBlanks (boolean, inherited) This attribute can be used to control the trimming of trailing space characters from fixed width character (CHAR) fields. No other field types are affected, even where field values have trailing spaces. The default is false (it is possible that that may change). Applications that need specific behaviour should set the attribute as needed. Emulation interfaces should set the attribute to match the behaviour of the interface they are emulating. Drivers are not required to support this attribute but any driver which does not must arrange to return undef as the attribute value. LongReadLen (unsigned integer, inherited) This attribute may be used to control the maximum length of 'long' ('blob', 'memo' etc.) fields which the driver will read from the database automatically when it fetches each row of data. A value of 0 means don't automatically fetch any long data (fetch should return undef for long fields when LongReadLen is 0). The default is typically 0 (zero) bytes but may vary between drivers. Most applications fetching long fields will set this value to slightly larger than the longest long field value which will be fetched. Changing the value of LongReadLen for a statement handle after it's been prepare()'d will typically have no 22/Jul/99 Last change: perl 5.005, patch 02 18 User Contributed Perl Documentation DBI(3) effect so it's usual to set LongReadLen on the $dbh before calling prepare. The LongReadLen attribute only relates to fetching/reading long values it is not involved in inserting/updating them. See the LongTruncOk entry elsewhere in this documentabout truncation behaviour. LongTruncOk (boolean, inherited) This attribute may be used to control the effect of fetching a long field value which has been truncated (typically because it's longer than the value of the LongReadLen attribute). By default LongTruncOk is false and fetching a truncated long value will cause the fetch to fail. (Applications should always take care to check for errors after a fetch loop in case an error, such as a divide by zero or long field truncation, caused the fetch to terminate prematurely.) If a fetch fails due to a long field truncation when LongTruncOk is false, many drivers will allow you to continue fetching further rows. See also the LongReadLen entry elsewhere in this document. private_* The DBI provides a way to store extra information in a DBI handle as 'private' attributes. The DBI will allow you to store and retreive any attribute which has a name starting with 'private_'. It is strongly recommended that you use just one private attribute (e.g., use a hash ref) and give it a long and unambiguous name that includes the module or application that the attribute relates to (e.g., 'private_YourFullModuleName_thingy'). DBI DATABASE HANDLE OBJECTS Database Handle Methods selectrow_array @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values); This utility method combines the prepare, execute, and fetchrow_array entries elsewhere in this documentinto a single call. If called in a list context it returns the 22/Jul/99 Last change: perl 5.005, patch 02 19 User Contributed Perl Documentation DBI(3) first row of data from the statement. If called in a scalar context it returns the first field of the first row. The $statement parameter can be a previously prepared statement handle in which case the prepare is skipped. In any method fails, and the RaiseError entry elsewhere in this documentis not set, selectrow_array will return an empty list (or undef in scalar context). selectall_arrayref $ary_ref = $dbh->selectall_arrayref($statement); $ary_ref = $dbh->selectall_arrayref($statement, \%attr); $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values); This utility method combines the prepare, execute, and fetchall_arrayref entries elsewhere in this documentinto a single call. The $statement parameter can be a previously prepared statement handle in which case the prepare is skipped. In any method fails, and the RaiseError entry elsewhere in this documentis not set, selectall_arrayref will return undef. prepare $sth = $dbh->prepare($statement) || die $dbh->errstr; $sth = $dbh->prepare($statement, \%attr) || die $dbh->errstr; Prepares a single statement for later execution by the database engine and returns a reference to a statement handle object. The returned statement handle can be used to get attributes of the statement and invoke the the execute entry elsewhere in this documentmethod. See the section on /Statement Handle Methods. Note that prepare should never execute a statement, even if it is not a select statement, it only prepares it for execution. (Having said that, some drivers, notably Oracle, will execute data definition statements such as create/drop table when they are prepared. In practice this is rarely a problem.) Drivers for engines which don't have the concept of preparing a statement will typically just store the statement in the returned handle and process it when $sth->execute is called. Such drivers are likely to be unable to give much useful information about the 22/Jul/99 Last change: perl 5.005, patch 02 20 User Contributed Perl Documentation DBI(3) statement, such as $sth->{NUM_OF_FIELDS}, until after $sth->execute has been called. Portable applications should take this into account. In general DBI drivers do not parse the contents of the statement (other than simply counting any the Placeholders entry elsewhere in this document). The statement is passed directly to the database engine (sometimes known as pass-thru mode). This has advantages and disadvantages. On the plus side, you can access all the functionality of the engine being used. On the downside, you're limited if using a simple engine and need to take extra care if attempting to write applications to be portable between engines. Some command-line SQL tools use statement terminators, like a semicolon, to indicate the end of a statement. Such terminators should not be used with the DBI. prepare_cached $sth = $dbh->prepare_cached($statement) || die $dbh->errstr; $sth = $dbh->prepare_cached($statement, \%attr) || die $dbh->errstr; Like the prepare entry elsewhere in this documentexcept that the statement handled returned will be stored in a hash associated with the $dbh. If another call is made to prepare_cached with the same parameter values then the corresponding cached $sth will be returned (and the database server will not be contacted). This cacheing can be useful in some applications but it can also cause problems and should be used with care. Currently a warning will be generated if the cached $sth being returned is active (i.e., is a select that may still have data to be fetched). The cache can be accessed (and cleared) via the the CachedKids entry elsewhere in this documentattribute. do $rc = $dbh->do($statement) || die $dbh->errstr; $rc = $dbh->do($statement, \%attr) || die $dbh->errstr; $rv = $dbh->do($statement, \%attr, @bind_values) || ... Prepare and execute a single statement. Returns the number of rows affected (-1 if not known or not available) or undef on error. This method is typically most useful for non-select statements which either cannot be prepared in advance 22/Jul/99 Last change: perl 5.005, patch 02 21 User Contributed Perl Documentation DBI(3) (due to a limitation of the driver) or which do not need to be executed repeatedly. It should not be used for select statements. The default do method is logically similar to: sub do { my($dbh, $statement, $attr, @bind_values) = @_; my $sth = $dbh->prepare($statement, $attr) or return undef; $sth->execute(@bind_values) or return undef; my $rows = $sth->rows; ($rows == 0) ? "0E0" : $rows; # always return true if no error } Example: my $rows_deleted = $dbh->do(q{ delete from table where status = ? }, undef, 'DONE') || die $dbh->errstr; Using placeholders and @bind_values with the do method can be useful because it avoids the need to correctly quote any variables in the $statement. The q{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like qq{...} operator if you want to interpolate variables into the string. See the section on Quote and Quote-like Operators in the perlop manpage for more details. commit $rc = $dbh->commit || die $dbh->errstr; Commit (make permanent) the most recent series of database changes if the database supports transactions. If the database supports transactions and AutoCommit is on then the commit should issue a "commit ineffective with AutoCommit" warning. See also the Transactions entry elsewhere in this document. rollback $rc = $dbh->rollback || die $dbh->errstr; Roll-back (undo) the most recent series of uncommitted database changes if the database supports transactions. 22/Jul/99 Last change: perl 5.005, patch 02 22 User Contributed Perl Documentation DBI(3) If the database supports transactions and AutoCommit is on then the rollback should issue a "rollback ineffective with AutoCommit" warning. See also the Transactions entry elsewhere in this document. disconnect $rc = $dbh->disconnect || warn $dbh->errstr; Disconnects the database from the database handle. Typically only used before exiting the program. The handle is of little use after disconnecting. The transaction behaviour of the disconnect method is, sadly, undefined. Some database systems (such as Oracle and Ingres) will automatically commit any outstanding changes, but others (such as Informix) will rollback any outstanding changes. Applications should explicitly call commit or rollback before calling disconnect. The database is automatically disconnected (by the DESTROY method) if still connected when there are no longer any references to the handle. The DESTROY method for each driver should explicitly call rollback to undo any uncommitted changes. This is vital behaviour to ensure that incomplete transactions don't get committed simply because Perl calls DESTROY on every object before exiting. Also, do not rely on the order of object destruction during 'global destruction', it is undefined. If you disconnect from a database while you still have active statement handles you will get a warning. The statement handles should either be cleared (destroyed) before disconnecting or the finish method called on each one. ping $rc = $dbh->ping; Attempts to determine, in a reasonably efficient way, if the database server is still running and the connection to it is still working. The default implementation currently always returns true without actually doing anything. Individual drivers should implement this function in the most suitable manner for their database engine. 22/Jul/99 Last change: perl 5.005, patch 02 23 User Contributed Perl Documentation DBI(3) Very few applications would have any use for this method. See the specialist Apache::DBI module for one example usage. table_info *NEW* Warning: This method is experimental and may change or disappear. $sth = $dbh->table_info; Returns an active statement handle that can be used to fetch information about tables and views that exist in the database. The handle has at least the following fields in the order show below. Other fields, after these, may also be present. TABLE_QUALIFIER: Table qualifier identifier. NULL (undef) if not applicable to data source (usually the case). Empty if not applicable to the table. TABLE_OWNER: Table owner identifier. NULL (undef) if not applicable to data source. Empty if not applicable to the table. TABLE_NAME: Table name. TABLE_TYPE: One of the following: "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" or a data source specific type identifier. REMARKS: A description of the table. May be NULL (undef). Note that table_info might not return records for all tables. Applications can use any valid table regardless of whether it's returned by table_info. See also the tables entry elsewhere in this document. tables *NEW* Warning: This method is experimental and may change or disappear. @names = $dbh->tables; Returns a list of table and view names. This list should include all tables which can be used in a select statement without further qualification. That typically means all the tables and views owned by the current user and all those accessible via public synonyms/aliases 22/Jul/99 Last change: perl 5.005, patch 02 24 User Contributed Perl Documentation DBI(3) (excluding non-metadata system tables and views). Note that table_info might not return records for all tables. Applications can use any valid table regardless of whether it's returned by tables. See also the table_info entry elsewhere in this document. type_info_all *NEW* Warning: This method is experimental and may change or disappear. $type_info_all = $dbh->type_info_all; Returns a reference to an array which holds information about each data type variant supported by the database and driver. The first item is a reference to a hash of Name => Index pairs. The following items are references to arrays, one per supported data type variant. The leading hash defines the names and order of the fields within the following list of arrays. For example: $type_info_all = [ { TYPE_NAME => 0, DATA_TYPE => 1, PRECISION => 2, LITERAL_PREFIX => 3, LITERAL_SUFFIX => 4, CREATE_PARAMS => 5, NULLABLE => 6, CASE_SENSITIVE => 7, SEARCHABLE => 8, UNSIGNED_ATTRIBUTE=> 9, MONEY => 10, AUTO_INCREMENT => 11, LOCAL_TYPE_NAME => 12, MINIMUM_SCALE => 13, MAXIMUM_SCALE => 14, }, [ 'VARCHAR', SQL_VARCHAR, undef, "'","'", undef,0, 1,1,0,0,0,undef,1,255 ], [ 'INTEGER', SQL_INTEGER, undef, "", "", undef,0, 0,1,0,0,0,undef,0, 0 ], ]; Note that more than one row may have the same value in the DATA_TYPE field. This method is not normally used directly. The the 22/Jul/99 Last change: perl 5.005, patch 02 25 User Contributed Perl Documentation DBI(3) type_info entry elsewhere in this documentmethod provides a more useful interface to the data. The meaning of the fields is described in the documentation for the the type_info entry elsewhere in this documentmethod. type_info *NEW* Warning: This method is experimental and may change or disappear. @type_info = $dbh->type_info($data_type); Returns a list of hash references holding information about one or more variants of $data_type (or a type reasonably compatible with it). If $data_type is SQL_ALL_TYPES then the list will contain hashes for all data type variants supported by the database and driver. The keys of the hash follow the same letter case conventions as the rest of the DBI (see the section on /Naming Conventions and Name Space). The following items should exist: TYPE_NAME (string) Data type name for use in CREATE TABLE statements etc. DATA_TYPE (integer) SQL data type number. PRECISION (integer) The maximum precision of the data type. NULL (undef) is returned for data types where this is not applicable. LITERAL_PREFIX (string) Characters used to prefix a literal. Typically "'" for characters, possibly "0x" for binary values passed as hex. NULL (undef) is returned for data types where this is not applicable. LITERAL_SUFFIX (string) Characters used to suffix a literal. Typically "'" for characters. NULL (undef) is returned for data types where this is not applicable. CREATE_PARAMS (string) Parameters for a data type definition. For example, CREATE_PARAMS for a DECIMAL would be 22/Jul/99 Last change: perl 5.005, patch 02 26 User Contributed Perl Documentation DBI(3) "precision,scale". For a VARCHAR it would be "max length". NULL (undef) is returned for data types where this is not applicable. NULLABLE (integer) Indicates whether the data type accepts a NULL value: 0 = no, 1 = yes, 2 = unknown. CASE_SENSITIVE (boolean) Indicates whether the data type is case sensitive in collations and comparisons. SEARCHABLE (integer) Indicates how the data type can be used in a WHERE clause: 0 - cannot be used in a WHERE clause 1 - only with a LIKE predicate 2 - all comparison operators except LIKE 3 - can be used in a WHERE clause with any comparison operator UNSIGNED_ATTRIBUTE (boolean) Indicates whether the data type is unsigned. NULL (undef) is returned for data types where this is not applicable. MONEY (boolean) Indicates whether the data type is a money data type. NULL (undef) is returned for data types where this is not applicable. AUTO_INCREMENT (boolean) Indicates whether the data type is autoincrementing. NULL (undef) is returned for data types where this is not applicable. LOCAL_TYPE_NAME (string) Localised version of the TYPE_NAME for use in dialogue with users. NULL (undef) is returned if a localised name is not available (in which case TYPE_NAME should be used). MINIMUM_SCALE (integer) The minimum scale of the data type. If a data type has a fixed scale then MAXIMUM_SCALE holds the same value. NULL (undef) is returned for data types where this is not applicable. MAXIMUM_SCALE (integer) The maximum scale of the data type. If a data type has a fixed scale then MINIMUM_SCALE holds the same 22/Jul/99 Last change: perl 5.005, patch 02 27 User Contributed Perl Documentation DBI(3) value. NULL (undef) is returned for data types where this is not applicable. quote $sql = $dbh->quote($value); $sql = $dbh->quote($value, $data_type); Quote a string literal for use as a literal value in an SQL statement by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks. $sql = sprintf "select foo from bar where baz = %s", $dbh->quote("Don't\n"); For most database types quote would return 'Don''t' (including the outer quotation marks). An undefined $value value will be returned as the string NULL (without quotation marks). If $data_type is supplied it is used to try to determine the required quoting behaviour by using the information returned by the type_info entry elsewhere in this document. As a special case, the standard numeric types are optimised to return $value without calling type_info. Quote may not be able to deal with all possible input (such as binary data) and is not related in any way with escaping or quoting shell meta-characters. There is no need to quote values being used with the section on /"Placeholders and Bind Values. Database Handle Attributes This section describes attributes specific to database handles. Changes to these database handle attributes do not affect any other existing or future database handles. Attempting to set or get the value of an unknown attribute is fatal, except for private driver specific attributes (which all have names starting with a lowercase letter). Example: $h->{AutoCommit} = ...; # set/write ... = $h->{AutoCommit}; # get/read 22/Jul/99 Last change: perl 5.005, patch 02 28 User Contributed Perl Documentation DBI(3) AutoCommit (boolean) If true then database changes cannot be rolled-back (undone). If false then database changes automatically occur within a 'transaction' which must either be committed or rolled-back using the commit or rollback methods. Drivers should always default to AutoCommit mode. (An unfortunate choice forced on the DBI by ODBC and JDBC conventions.) Attempting to set AutoCommit to an unsupported value is a fatal error. This is an important feature of the DBI. Applications which need full transaction behaviour can set $dbh->{AutoCommit}=0 (or via connect) without having to check the value was assigned okay. For the purposes of this description we can divide databases into three categories: Database which don't support transactions at all. Database in which a transaction is always active. Database in which a transaction must be explicitly started ('BEGIN WORK'). * Database which don't support transactions at all For these databases attempting to turn AutoCommit off is a fatal error. Commit and rollback both issue warnings about being ineffective while AutoCommit is in effect. * Database in which a transaction is always active These are typically mainstream commercial relational databases with 'ANSI standard' transaction behaviour. If AutoCommit is off then changes to the database won't have any lasting effect unless the commit entry elsewhere in this documentis called (but see also the disconnect entry elsewhere in this document). If the rollback entry elsewhere in this documentis called then any changes since the last commit are undone. If AutoCommit is on then the effect is the same as if the DBI were to have called commit automatically after every successful database operation. In other words, calling commit or rollback explicitly while AutoCommit is on would be ineffective because the changes would have already been commited. Changing AutoCommit from off to on should issue a the commit entry elsewhere in this documentin most drivers. 22/Jul/99 Last change: perl 5.005, patch 02 29 User Contributed Perl Documentation DBI(3) Changing AutoCommit from on to off should have no immediate effect. For databases which don't support a specific auto-commit mode, the driver has to commit each statement automatically using an explicit COMMIT after it completes successfully (and roll it back using an explicit ROLLBACK if it fails). The error information reported to the application will correspond to the statement which was executed, unless it succeeded and the commit or rollback failed. * Database in which a transaction must be explicitly started For these database the intention is to have them act like databases in which a transaction is always active (as described above). To do this the DBI driver will automatically begin a transaction when AutoCommit is turned off (from the default on state) and will automatically begin another transaction after a the commit entry elsewhere in this documentor the rollback entry elsewhere in this document. In this way, the application does not have to treat these databases as a special case. Name (string) Holds the 'name' of the database. Usually (and recommended to be) the same as the "dbi:DriverName:..." string used to connect to the database but with the leading "dbi:DriverName:" removed. RowCacheSize (integer) *NEW* A hint to the driver indicating the size of local row cache the application would like the driver to use for future select statements. If a row cache is not implemented then setting RowCacheSize is ignored and getting the value returns undef. Some RowCacheSize values have special meaning: 0 - Automatically determine a reasonable cache size for each select 1 - Disable the local row cache >1 - Cache this many rows <0 - Cache as many rows fit into this much memory for each select. Note that large cache sizes may require very large amount of memory (cached rows * maximum size of row) and that a large cache will cause a longer delay for the 22/Jul/99 Last change: perl 5.005, patch 02 30 User Contributed Perl Documentation DBI(3) first fetch and when the cache needs refilling. See also the RowsInCache entry elsewhere in this documentstatement handle attribute. DBI STATEMENT HANDLE OBJECTS Statement Handle Methods bind_param $rc = $sth->bind_param($p_num, $bind_value) || die $sth->errstr; $rv = $sth->bind_param($p_num, $bind_value, \%attr) || ... $rv = $sth->bind_param($p_num, $bind_value, $bind_type) || ... The bind_param method can be used to bind (assign/associate) a value with a placeholder embedded in the prepared statement. Placeholders are indicated with question mark character (?). For example: $dbh->{RaiseError} = 1; # save having to check each method call $sth = $dbh->prepare("select name, age from people where name like ?"); $sth->bind_param(1, "John%"); # placeholders are numbered from 1 $sth->execute; DBI::dump_results($sth); Note that the ? is not enclosed in quotation marks even when the placeholder represents a string. Some drivers also allow :1, :2 etc and :name style placeholders in addition to ? but their use is not portable. Some drivers do not support placeholders. With most drivers placeholders can't be used for any element of a statement that would prevent the database server validating the statement and creating a query execution plan for it. For example: "select name, age from ?" # wrong "select name, ? from people" # wrong Also, placeholders can only represent single scalar values, so this statement, for example, won't work as expected for more than one value: "select name, age from people where name in (?)" # wrong The \%attr parameter can be used to specify the data type the placeholder should have. Typically the driver is only interested in knowing if the placeholder should be bound as a number or a string. $sth->bind_param(1, $value, { TYPE => SQL_INTEGER }); 22/Jul/99 Last change: perl 5.005, patch 02 31 User Contributed Perl Documentation DBI(3) As a short-cut for this common case, the data type can be passed directly inplace of the attr hash reference. This example is equivalent to the one above: $sth->bind_param(1, $value, SQL_INTEGER); The TYPE cannot be changed after the first bind_param call (but it can be left unspecified, in which case it defaults to the previous value). The TYPE value indicated the standard non-driver-specific type for this parameter. To specify the driver-specific type the driver may support a driver-specific attribute, e.g., { ora_type => 97 }. Perl only has string and number scalar data types. All database types that aren't numbers are bound as strings and must be in a format the database will understand. Undefined values or undef are be used to indicate null values. bind_param_inout $rc = $sth->bind_param_inout($p_num, \$bind_value, $max_len) || die $sth->errstr; $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, \%attr) || ... $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, $bind_type) || ... This method acts like the bind_param entry elsewhere in this documentbut also enables values to be output from (updated by) the statement. The statement is typically a call to a stored procedure. The $bind_value must be passed as a reference to the actual value to be used. Note that unlike the bind_param entry elsewhere in this documentthe $bind_value variable is not read when bind_param_inout is called. Instead, the value in the variable is read at the time the execute entry elsewhere in this documentis called. The additional $max_len parameter specifies the minimum amount of memory to allocate to $bind_value for the new value. If the value is too big to fit then the execute should fail. If unsure what value to use, pick a generous length larger than the longest value that would ever be returned. The only cost of using a very large value is memory. It is expected that few drivers will support this method. The only driver currently known to do so is DBD::Oracle (DBD::ODBC may support it in a future release). Therefore it should not be used for database independent applications. 22/Jul/99 Last change: perl 5.005, patch 02 32 User Contributed Perl Documentation DBI(3) execute $rv = $sth->execute || die $sth->errstr; $rv = $sth->execute(@bind_values) || die $sth->errstr; Perform whatever processing is necessary to execute the prepared statement. An undef is returned if an error occurs, a successful execute always returns true regardless of the number of rows affected (even if it's zero, see below). It is always important to check the return status of execute (and most other DBI methods) for errors. For a non-select statement, execute returns the number of rows affected (if known). If no rows were affected then execute returns "0E0" which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known then execute returns -1. For select statements execute simply 'starts' the query within the Engine. Use one of the fetch methods to retreive the data after calling execute. The execute method does not return the number of rows that will be returned by the query (because most Engines can't tell in advance), it simply returns a true value. If any arguments are given then execute will effectively call the bind_param entry elsewhere in this documentfor each value before executing the statement. Values bound in this way are usually treated as SQL_VARCHAR types unless the driver can determine the correct type (which is rare) or bind_param (or bind_param_inout) has already been used to specify the type. fetchrow_arrayref $ary_ref = $sth->fetchrow_arrayref; $ary_ref = $sth->fetch; # alias Fetches the next row of data and returns a reference to an array holding the field values. Null field values are returned as undef. This is the fastest way to fetch data, particularly if used with $sth->bind_columns. If there are no more rows or an error occurs fetchrow_arrayref returns undef (you should check $sth->err afterwards or use the RaiseError entry elsewhere in this document). Note that currently the same array ref will be returned for each fetch so don't store the ref and then use it 22/Jul/99 Last change: perl 5.005, patch 02 33 User Contributed Perl Documentation DBI(3) after a later fetch. fetchrow_array @ary = $sth->fetchrow_array; An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as an array holding the field values. Null values are returned as undef. If there are no more rows or an error occurs fetchrow_array returns an empty list (you should check $sth->err afterwards or use the RaiseError entry elsewhere in this document). fetchrow_hashref $hash_ref = $sth->fetchrow_hashref; $hash_ref = $sth->fetchrow_hashref($name); An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs. Null values are returned as undef. If there are no more rows or an error occurs fetchrow_hashref returns undef (you should check $sth->err afterwards or use the RaiseError entry elsewhere in this document). The optional $name parameter specifies the name of the statement handle attribute to use to get the field names. It defaults to 'the NAME entry elsewhere in this document'. The keys of the hash are the same names returned by $sth->{$name}. If more than one field has the same name there will only be one entry in the returned hash for those fields. Note that using fetchrow_hashref is currently not portable between databases because different databases return fields names with different letter cases (some all uppercase, some all lower, and some return the letter case used to create the table). This will be addressed in a future version of the DBI. Because of the extra work fetchrow_hashref and perl have to perform it is not as efficient as fetchrow_arrayref or fetchrow_array and is not recommended where performance is very important. Currently a new hash reference is returned for each row. This is likely to 22/Jul/99 Last change: perl 5.005, patch 02 34 User Contributed Perl Documentation DBI(3) change in the future so don't rely on it. fetchall_arrayref $tbl_ary_ref = $sth->fetchall_arrayref; $tbl_ary_ref = $sth->fetchall_arrayref( $slice_array_ref ); $tbl_ary_ref = $sth->fetchall_arrayref( $slice_hash_ref ); The fetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array which contains one reference per row. If there are no rows to return, fetchall_arrayref returns a reference to an empty array. If an error occurs fetchall_arrayref returns the data fetched thus far (you should check $sth->err afterwards or use the RaiseError entry elsewhere in this document). When passed an array reference, fetchall_arrayref uses the fetchrow_arrayref entry elsewhere in this documentto fetch each row as an array ref. If the parameter array is not empty then it is used as a slice to select individual columns by index number. With no parameters, fetchall_arrayref acts as if passed an empty array ref. When passed a hash reference, fetchall_arrayref uses the fetchrow_hashref entry elsewhere in this documentto fetch each row as a hash ref. If the parameter hash is not empty then it is used as a slice to select individual columns by name. The names should be lower case regardless of the letter case in $sth->{NAME}. The values of the hash should be set to 1. For example, to fetch just the first column of every row you can use: $tbl_ary_ref = $sth->fetchall_arrayref([0]); To fetch the second to last and last column of every row you can use: $tbl_ary_ref = $sth->fetchall_arrayref([-2,-1]); To fetch only the fields called foo and bar of every row you can use: $tbl_ary_ref = $sth->fetchall_arrayref({ foo=>1, bar=>1 }); The first two examples return a ref to an array of array refs. The last returns a ref to an array of hash refs. 22/Jul/99 Last change: perl 5.005, patch 02 35 User Contributed Perl Documentation DBI(3) finish $rc = $sth->finish; Indicates that no more data will be fetched from this statement handle before it is either executed again or destroyed. It is rarely needed but can sometimes be helpful in order to allow the server to free up resources currently being held (such as sort buffers). When all the data has been fetched from a select statement the driver should automatically call finish for you. So you should not normally need to call it explicitly. Consider a query like SELECT foo FROM table WHERE bar=? ORDER BY foo where you want to select just the first (smallest) foo value from a large table. When executed the database server will have to use temporary buffer space to store the sorted rows. If, after executing the handle and selecting one row, the handle won't be re-executed for some time or not at all, the finish method can be used to tell the server that the buffer space can be freed. Calling finish resets the the Active entry elsewhere in this documentattribute for the statement. It may also make some statement handle attributes, like NAME and TYPE etc., unavailable if they have not already been accessed (and thus cached). The finish method does not affect the transaction status of the session. It has nothing to do with transactions. It's mostly an internal 'housekeeping' method that is rarely needed. There's no need to call finish if you're about to destroy or re-execute the statement handle. See also the disconnect entry elsewhere in this document and the the Active entry elsewhere in this documentattribute. rows $rv = $sth->rows; Returns the number of rows affected by the last database altering command, or -1 if not known or not available. Generally you can only rely on a row count after a do or non-select execute (for some specific operations like update and delete) or after fetching all the rows of a 22/Jul/99 Last change: perl 5.005, patch 02 36 User Contributed Perl Documentation DBI(3) select statement. For select statements it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far but others may return -1 until all rows have been fetched. So use of the rows method with select statements is not recommended. bind_col $rc = $sth->bind_col($column_number, \$var_to_bind); $rc = $sth->bind_col($column_number, \$var_to_bind, \%attr); Binds an output column (field) of a select statement to a perl variable. You do not need to do this but it can be useful for some applications. Whenever a row is fetched from the database the corresponding perl variable is automatically updated. There is no need to fetch and assign the values manually. This makes using bound variables very efficient. See bind_columns below for an example. Note that column numbers count up from 1. The binding is performed at a very low level using perl aliasing so there is no extra copying taking place. So long as the driver uses the correct internal DBI call to get the array the fetch function returns, it will automatically support column binding. For maximum portability between drivers, bind_col should be called after execute. The the bind_param entry elsewhere in this documentmethod performs a similar function for input variables. See also the section on /"Placeholders and Bind Values for more information. bind_columns $rc = $sth->bind_columns(\%attr, @list_of_refs_to_vars_to_bind); Calls bind_col for each column of the select statement. bind_columns will die if the number of references does not match the number of fields. You do not need to do this but it can be useful for some applications. For maximum portability between drivers, bind_columns should be called after execute. For example: 22/Jul/99 Last change: perl 5.005, patch 02 37 User Contributed Perl Documentation DBI(3) $dbh->{RaiseError} = 1; # do this, or check every call for errors $sth = $dbh->prepare(q{ select region, sales from sales_by_region }); $sth->execute; my ($region, $sales); # Bind perl variables to columns: $rv = $sth->bind_columns(undef, \$region, \$sales); # you can also use perl's \(...) syntax (see perlref docs): # $sth->bind_columns(undef, \($region, $sales)); # Column binding is the most efficient way to fetch data while ($sth->fetch) { print "$region: $sales\n"; } dump_results $rows = $sth->dump_results($maxlen, $lsep, $fsep, $fh); Fetches all the rows from $sth, calls DBI::neat_list for each row and prints the results to $fh (defaults to STDOUT) separated by $lsep (default "\n"). $fsep defaults to ", " and $maxlen defaults to 35. This method is designed as a handy utility for prototyping and testing queries. Since it uses the neat_list entry elsewhere in this documentwhich uses the neat entry elsewhere in this documentwhich formats and edits the string for reading by humans, it's not recomended for data transfer applications. Statement Handle Attributes This section describes attributes specific to statement handles. Most of these attributes are read-only. Changes to these statement handle attributes do not affect any other existing or future statement handles. Attempting to set or get the value of an unknown attribute is fatal, except for private driver specific attributes (which all have names starting with a lowercase letter). Example: ... = $h->{NUM_OF_FIELDS}; # get/read Note that some drivers cannot provide valid values for some or all of these attributes until after $sth->execute has been called. 22/Jul/99 Last change: perl 5.005, patch 02 38 User Contributed Perl Documentation DBI(3) See also the finish entry elsewhere in this documentfor the effect it may have on some attributes. NUM_OF_FIELDS (integer, read-only) Number of fields (columns) the prepared statement will return. Non-select statements will have NUM_OF_FIELDS == 0. NUM_OF_PARAMS (integer, read-only) The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details. NAME (array-ref, read-only) Returns a reference to an array of field names for each column. The names may contain spaces but should not be truncated or have any trailing space. print "First column name: $sth->{NAME}->[0]\n"; TYPE (array-ref, read-only) *NEW* Returns a reference to an array of integer values for each column. The value indicates the data type of the corresponding column. The values used correspond to the international standards (ANSI X3.135 and ISO/IEC 9075) which, in general terms means ODBC. Driver specific types which don't exactly match standard types should generally return the same values as an ODBC driver supplied by the makers of the database. That might include private type numbers the vendor has officially registered. See: ftp://jerry.ece.umassd.edu/isowg3/dbl/SQL_Registry Where there's no vendor supplied ODBC driver to be compatible with the DBI driver can use type numbers in the range now officially reserved for use by the DBI: -9999 to -9000. All possible values for TYPE should have at least one entry in the output of the the type_info_all entry elsewhere in this documentmethod. PRECISION (array-ref, read-only) *NEW* Returns a reference to an array of integer values for each column. For nonnumeric columns the value generally refers to either the maximum length or the defined length of the column. For numeric columns the value refers to the maximum number of digits used by the data type (without considering a sign character or decimal 22/Jul/99 Last change: perl 5.005, patch 02 39 User Contributed Perl Documentation DBI(3) point). Note that for floating point types (REAL, FLOAT, DOUBLE) the 'display size' can be up to 7 characters greater than the precision (for the sign + decimal point + the letter E + a sign + 2 or 3 digits). SCALE (array-ref, read-only) *NEW* Returns a reference to an array of integer values for each column. NULL (undef) values indicate columns where scale is not applicable. NULLABLE (array-ref, read-only) Returns a reference to an array indicating the possibility of each column returning a null: 0 = no, 1 = yes, 2 = unknown. print "First column may return NULL\n" if $sth->{NULLABLE}->[0]; CursorName (string, read-only) Returns the name of the cursor associated with the statement handle if available. If not available or the database driver does not support the "where current of ..." SQL syntax then it returns undef. Statement (string, read-only) *NEW* Returns the statement string passed to the the prepare entry elsewhere in this documentmethod. RowsInCache (integer, read-only) *NEW* If the driver supports a local row cache for select statements then this attribute holds the number of un- fetched rows in the cache. If the driver doesn't, then it returns undef. Note that some drivers pre-fetch rows on execute, others wait till the first fetch. See also the the RowCacheSize entry elsewhere in this documentdatabase handle attribute. FURTHER INFORMATION Transactions Transactions are a fundamental part of any robust database system. They protect against errors and database corruption by ensuring that sets of related changes to the database take place in atomic (indivisible, all-or-nothing) units. This section applies to databases which support transactions and where AutoCommit is off. See the AutoCommit entry elsewhere in this documentfor details of using AutoCommit with various types of database. 22/Jul/99 Last change: perl 5.005, patch 02 40 User Contributed Perl Documentation DBI(3) The recommended way to implement robust transactions in Perl applications is to make use of eval { ... } (which is very fast, unlike eval "..."). eval { foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates }; if ($@) { # $@ contains $DBI::errstr if DBI RaiseError caused die $dbh->rollback; # add other application on-error-clean-up code here } else { $dbh->commit; } The code in foo(), or any other code executed from within the curly braces, can be implemented in this way: $h->method(@args) || die $h->errstr or the $h->{RaiseError} attribute can be set on. With RaiseError set the DBI will automatically die if any DBI method call on that handle (or a child handle) fails, so you don't have to test the return value of each method call. See the RaiseError entry elsewhere in this documentfor more details. A major advantage of the eval approach is that the transaction will be properly rolled back if any code in the inner application dies for any reason. The major advantage of using the $h->{RaiseError} attribute is that all DBI calls will be checked automatically. Both techniques are strongly recommended. Handling BLOB / LONG / Memo Fields Many databases support 'blob' (binary large objects), 'long' or similar datatypes for holding very long strings or large amounts of binary data in a single field. Some databases support variable length long values over 2,000,000,000 bytes in length. Since values of that size can't usually be held in memory and because databases can't usually know in advance the length of the longest long that will be returned from a select statement (unlike other data types) some special handling is required. 22/Jul/99 Last change: perl 5.005, patch 02 41 User Contributed Perl Documentation DBI(3) In this situation the value of the $h->{LongReadLen} attribute is used to determine how much buffer space to allocate when fetching such fields. The $h->{LongTruncOk} attribute is used to determine how to behave if a fetched value can't fit into the buffer. When trying to insert long or binary values, placeholders should be used since there are often limits on the maximum size of an (insert) statement and the the quote entry elsewhere in this documentmethod generally can't cope with binary data. See the section on /Placeholders and Bind Values. Simple Examples Here's a complete example program to select and fetch some data: my $dbh = DBI->connect("dbi:DriverName:db_name", $user, $password) || die "Can't connect to $data_source: $DBI::errstr"; my $sth = $dbh->prepare( q{ SELECT name, phone FROM mytelbook }) || die "Can't prepare statement: $DBI::errstr"; my $rc = $sth->execute || die "Can't execute statement: $DBI::errstr"; print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n"; print "Field names: @{ $sth->{NAME} }\n"; while (($name, $phone) = $sth->fetchrow_array) { print "$name: $phone\n"; } # check for problems which may have terminated the fetch early die $sth->errstr if $sth->err; $dbh->disconnect; Here's a complete example program to insert some data from a file: (this example uses RaiseError to avoid needing to check each call) my $dbh = DBI->connect("dbi:DriverName:db_name", $user, $password, { RaiseError => 1, AutoCommit => 0 }); my $sth = $dbh->prepare( q{ INSERT INTO table (name, phone) VALUES (?, ?) }); 22/Jul/99 Last change: perl 5.005, patch 02 42 User Contributed Perl Documentation DBI(3) open FH, ") { chop; my ($name, $phone) = split /,/; $sth->execute($name, $phone); } close FH; $dbh->commit; $dbh->disconnect; Converting fetched NULLs (undefined values) to empty strings: while($row = $sth->fetchrow_arrayref) { # this is a fast and simple way to deal with nulls: foreach (@$row) { $_ = '' unless defined } print "@$row\n"; } The q{...} style quoting used in these example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like qq{...} operator if you want to interpolate variables into the string. See the section on Quote and Quote-like Operators in the perlop manpage for more details. Threads and Thread Safety Perl versions 5.004_50 and later support threads on many platforms. The DBI should build on these platforms but currently has made no attempt to be thread safe. Signal Handling and Canceling Operations The first thing to say is that signal handling in Perl is currently not safe. There is always a small risk of Perl crashing and/or core dumping when, or after, handling a signal. (The risk was reduced with 5.004_04 but is still present.) The two most common uses of signals in relation to the DBI are for canceling operations when the user types Ctrl-C (interrupt), and for implementing a timeout using alarm() and $SIG{ALRM}. To assist in implementing these operations the DBI provides a cancel method for statement handles. The cancel method should abort the current operation and is designed to be called from a signal handler. 22/Jul/99 Last change: perl 5.005, patch 02 43 User Contributed Perl Documentation DBI(3) However, it must be stressed that: a) few drivers implement this at the moment (the DBI provides a default method that just returns undef), b) even if implemented there is still a possibility that the statement handle, and possibly the parent database handle, will not be usable afterwards. If cancel returns true then it is implemented and has successfully invoked the database engine's own cancel function. If it returns false then cancel failed (undef if not implemented). DEBUGGING In addition to the the trace entry elsewhere in this documentmethod you can enable the same trace information by setting the DBI_TRACE environment variable before starting perl. On unix-like systems using a bourne-like shell you can do this easily for a single command: DBI_TRACE=2 perl your_test_script.pl If DBI_TRACE is set to a non-numeric value then it is assumed to be a file name and the trace level will be set to 2 with all trace output will be appended to that file. See also the the trace entry elsewhere in this documentmethod. WARNING AND ERROR MESSAGES (This section needs more words about causes and remedies.) Fatal Errors Can't call method "prepare" without a package or object reference The $dbh handle you're using to call prepare is probably undefined because the preceeding connect failed. You should always check the return status of DBI methods, or use the the RaiseError entry elsewhere in this documentattribute. Can't call method "execute" without a package or object reference The $sth handle you're using to call execute is probably undefined because the preceeding prepare failed. You should always check the return status of DBI methods, or use the the RaiseError entry elsewhere in this documentattribute. Database handle destroyed without explicit disconnect DBI/DBD internal version mismatch 22/Jul/99 Last change: perl 5.005, patch 02 44 User Contributed Perl Documentation DBI(3) DBD driver has not implemented the AutoCommit attribute Can't [sg]et %s->{%s}: unrecognised attribute panic: DBI active kids (%d) > kids (%d) panic: DBI active kids (%d) < 0 or > kids (%d) Warnings DBI Handle cleared whilst still holding %d cached kids! DBI Handle cleared whilst still active! DBI Handle has uncleared implementors data DBI Handle has %d uncleared child handles SEE ALSO Database Documentation SQL Language Reference Manual. Books and Journals Programming Perl 2nd Ed. by Larry Wall, Tom Christiansen & Randal Schwartz. Learning Perl by Randal Schwartz. Dr Dobb's Journal, November 1996. The Perl Journal, April 1997. Manual Pages the perl(1) manpage, the perlmod(1) manpage, the perlbook(1) manpage Mailing List The dbi-users mailing list is the primary means of communication among uses of the DBI and its related modules. Subscribe and unsubscribe via: http://www.fugue.com/dbi Mailing list archives are held at: http://www.rosat.mpe-garching.mpg.de/mailing-lists/PerlDB-Interest/ http://www.xray.mpe.mpg.de/mailing-lists/#dbi http://outside.organic.com/mail-archives/dbi-users/ http://www.coe.missouri.edu/~faq/lists/dbi.html 22/Jul/99 Last change: perl 5.005, patch 02 45 User Contributed Perl Documentation DBI(3) Assorted Related WWW Links The DBI 'Home Page' (not maintained by me): http://www.arcana.co.uk/technologia/perl/DBI Other related links: http://eskimo.tamu.edu/~jbaker/dbi-examples.html http://wdvl.com/Authoring/DB/Intro/toc.html http://www-ccs.cs.umass.edu/db.html http://www.odmg.org/odmg93/updates_dbarry.html http://www.jcc.com/sql_stnd.html ftp://alpha.gnu.ai.mit.edu/gnu/gnusql-0.7b3.tar.gz http://www.dbmsmag.com Data Warehouse Links http://www.datamining.org http://www.olapcouncil.org http://www.idwa.org http://www.knowledgecenters.org/dwcenter.asp http://pwp.starnetinc.com/larryg/ http://www.data-warehouse. Recommended Perl Programming Links http://language.perl.com/style/ FAQ Please also read the DBI FAQ which is installed as a DBI::FAQ module so you can use perldoc to read it by executing the perldoc DBI::FAQ command. AUTHORS DBI by Tim Bunce. This pod text by Tim Bunce, J. Douglas Dunlop, Jonathan Leffler and others. Perl by Larry Wall and the perl5-porters. COPYRIGHT The DBI module is Copyright (c) 1995,1996,1997 Tim Bunce. England. All rights reserved. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. ACKNOWLEDGEMENTS I would like to acknowledge the valuable contributions of the many people I have worked with on the DBI project, 22/Jul/99 Last change: perl 5.005, patch 02 46 User Contributed Perl Documentation DBI(3) especially in the early years (1992-1994). In no particular order: Kevin Stock, Buzz Moschetti, Kurt Andersen, Ted Lemon, William Hails, Garth Kennedy, Michael Peppler, Neil S. Briscoe, Jeff Urlwin, David J. Hughes, Jeff Stander, Forrest D Whitcher, Larry Wall, Jeff Fried, Roy Johnson, Paul Hudson, Georg Rehfeld, Steve Sizemore, Ron Pool, Jon Meek, Tom Christiansen, Steve Baumgarten, Randal Schwartz, and a whole lot more. TRANSLATIONS A German translation of this manual and other Perl module docs (all possibly slightly out of date) is available, thanks to O'Reilly, at: http://www.oreilly.de/catalog/perlmodger/manpages.html SUPPORT / WARRANTY The DBI is free software. IT COMES WITHOUT WARRANTY OF ANY KIND. Commercial support for Perl and the DBI, DBD::Oracle and Oraperl modules can be arranged via The Perl Clinic. See http://www.perlclinic.com for more details. OUTSTANDING ISSUES TO DO data types (ISO type numbers and type name conversions) error handling data dictionary methods test harness support methods portability blob_read etc FREQUENTLY ASKED QUESTIONS See the DBI FAQ for a more comprehensive list of FAQs. Use the perldoc DBI::FAQ command to read it. How fast is the DBI? To measure the speed of the DBI and DBD::Oracle code I modified DBD::Oracle such that you can set an attribute which will cause the same row to be fetched from the row cache over and over again (without involving Oracle code but exercising *all* the DBI and DBD::Oracle code in the code path for a fetch). The results (on my lightly loaded old Sparc 10) fetching 50000 rows using: 22/Jul/99 Last change: perl 5.005, patch 02 47 User Contributed Perl Documentation DBI(3) 1 while $csr->fetch; were: one field: 5300 fetches per cpu second (approx) ten fields: 4000 fetches per cpu second (approx) Obviously results will vary between platforms (newer faster platforms can reach around 50000 fetches per second) but it does give a feel for the maximum performance: fast. By way of comparison, using the code: 1 while @row = $csr->fetchrow_array; (fetchrow_array is roughly the same as ora_fetch) gives: one field: 3100 fetches per cpu second (approx) ten fields: 1000 fetches per cpu second (approx) Notice the slowdown and the more dramatic impact of extra fields. (The fields were all one char long. The impact would be even bigger for longer strings.) Changing that slightly to represent actually _doing_ something in perl with the fetched data: while(@row = $csr->fetchrow_array) { $hash{++$i} = [ @row ]; } gives: ten fields: 500 fetches per cpu second (approx) That simple addition has *halved* the performance. I therefore conclude that DBI and DBD::Oracle overheads are small compared with Perl language overheads (and probably database overheads). So, if you think the DBI or your driver is slow, try replacing your fetch loop with just: 1 while $csr->fetch; and time that. If that helps then point the finger at your own code. If that doesn't help much then point the finger at the database, the platform, the network etc. But think carefully before pointing it at the DBI or your driver. (Having said all that, if anyone can show me how to make the DBI or drivers even more efficient, I'm all ears.) 22/Jul/99 Last change: perl 5.005, patch 02 48 User Contributed Perl Documentation DBI(3) Why doesn't my CGI script work right? Read the information in the references below. Please do not post CGI related questions to the dbi-users mailing list (or to me). http://www.perl.com/perl/faq/idiots-guide.html http://www3.pair.com/webthing/docs/cgi/faqs/cgifaq.shtml http://www.perl.com/perl/faq/perl-cgi-faq.html http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html http://www.boutell.com/faq/ http://www.perl.com/perl/faq/ General problems and good ideas: Use the CGI::ErrorWrap module. Remember that many env vars won't be set for CGI scripts How can I maintain a WWW connection to a database? For information on the Apache httpd server and the mod_perl module see http://perl.apache.org/ A driver build fails because it can't find DBIXS.h The installed location of the DBIXS.h file changed with 0.77 (it was being installed into the 'wrong' directory but that's where driver developers came to expect it to be). The first thing to do is check to see if you have the latest version of your driver. Driver authors will be releasing new versions which use the new location. If you have the latest then ask for a new release. You can edit the Makefile.PL file yourself. Change the part which reads "-I.../DBI" so it reads "-I.../auto/DBI" (where ... is a string of non-space characters). Has the DBI and DBD::Foo been ported to NT / Win32? The latest version of the DBI and, at least, the DBD::Oracle module will build - without changes - on NT/Win32 if your are using the standard Perl 5.004 and not the ActiveWare port. Jeffrey Urlwin (or ) is helping me with the port (actually he's doing it and I'm integrating the changes :-). 22/Jul/99 Last change: perl 5.005, patch 02 49 User Contributed Perl Documentation DBI(3) What about ODBC? A DBD::ODBC module is available. Does the DBI have a year 2000 problem? No. The DBI has no knowledge or understanding of dates at all. Individual drivers (DBD::*) may have some date handling code but are unlikely to have year 2000 related problems within their code. However, your application code which uses the DBI and DBD drivers may have year 2000 related problems if it has not been designed and written well. See also the "Does Perl have a year 2000 problem?" section of the Perl FAQ: http://www.perl.com/CPAN/doc/FAQs/FAQ/PerlFAQ.html KNOWN DRIVER MODULES Altera - DBD::Altera Author: Dimitrios Souflis Email: dsouflis@altera.gr ODBC - DBD::ODBC Author: Tim Bunce Email: dbi-users@fugue.com Oracle - DBD::Oracle Author: Tim Bunce Email: dbi-users@fugue.com Ingres - DBD::Ingres Author: Henrik Tougaard Email: ht@datani.dk, dbi-users@fugue.com mSQL - DBD::mSQL DB2 - DBD::DB2 Empress - DBD::Empress 22/Jul/99 Last change: perl 5.005, patch 02 50 User Contributed Perl Documentation DBI(3) Informix - DBD::Informix Author: Jonathan Leffler Email: jleffler@informix.com, j.leffler@acm.org, dbi-users@fugue.com Solid - DBD::Solid Author: Thomas Wenrich Email: wenrich@site58.ping.at, dbi-users@fugue.com Postgres - DBD::Pg Author: Edmund Mergl Email: E.Mergl@bawue.de, dbi-users@fugue.com Illustra - DBD::Illustra Author: Peter Haworth Email: pmh@edison.ioppublishing.com, dbi-users@fugue.com Fulcrum SearchServer - DBD::Fulcrum Author: Davide Migliavacca Email: davide.migliavacca@inferentia.it XBase (dBase) - DBD::XBase Author: Honza Pazdziora Email: adelton@fi.muni.cz OTHER RELATED WORK AND PERL MODULES Apache::DBI by E.Mergl@bawue.de To be used with the Apache daemon together with an embedded perl interpreter like mod_perl. Establishes a database connection which remains open for the lifetime of the http daemon. This way the CGI connect and disconnect for every database access becomes superfluous. JDBC Server by Stuart 'Zen' Bishop The server is written in Perl. The client classes that talk to it are of course in Java. Thus, a Java applet or application will be able to comunicate via the JDBC API with any database that has a DBI driver installed. The URL used is in the form jdbc:dbi://host.domain.etc:999/Driver/DBName. It seems 22/Jul/99 Last change: perl 5.005, patch 02 51 User Contributed Perl Documentation DBI(3) to be very similar to some commercial products, such as jdbcKona. Remote Proxy DBD support Carl Declerck Terry Greenlaw Carl is developing a generic proxy object module which could form the basis of a DBD::Proxy driver in the future. Terry is doing something similar. SQL Parser Hugo van der Sanden Stephen Zander Based on the O'Reilly lex/yacc book examples and byacc. 22/Jul/99 Last change: perl 5.005, patch 02 52 User Contributed Perl Documentation DBI(3) 22/Jul/99 Last change: perl 5.005, patch 02 53 -- ___ // Zen (alias Stuart Bishop) Work: zen@cs.rmit.edu.au // E N Senior Systems Alchemist Play: zen@shangri-la.dropbear.id.au //__ Computer Science, RMIT WWW: http://www.cs.rmit.edu.au/~zen From infotechsys@pivot.net Mon Feb 7 07:59:37 2000 From: infotechsys@pivot.net (Wayne) Date: Mon, 07 Feb 2000 02:59:37 -0500 Subject: [DB-SIG] Why can't I append to a list object? Message-ID: <389E7B69.E9EEF893@pivot.net> Hello, I have some code that I can figure out why it dosen't work. The piece of code is this - . . . queryResults = db.query(mySelection) rptData = queryResults.dictresult() rptData.append = ({'EOF':'EOF'}) <--- this is the statement I get my error. The error I get is: rptData.append = ({'EOF':'EOF'}) TypeError: object has read-only attributes From what I read I thought one could append onto a list. A side question- What I'm trying to do is reconize when I read all the data from the list. I use 'for' loop but have found nothing in the material I read that would indicate how to determine the end of the list. TIA. Wayne From tbryan@server.python.net Wed Feb 9 02:57:10 2000 From: tbryan@server.python.net (Tom Bryan) Date: Tue, 8 Feb 2000 21:57:10 -0500 (EST) Subject: [DB-SIG] Why can't I append to a list object? In-Reply-To: <389E7B69.E9EEF893@pivot.net> Message-ID: On Mon, 7 Feb 2000, Wayne wrote: > queryResults = db.query(mySelection) > > rptData = queryResults.dictresult() dictresult() returns a dictionary or a list? > rptData.append = ({'EOF':'EOF'}) I'm guessing that rptData is a list. You meant to write rptData.append({'EOF':'EOF}) > The error I get is: > rptData.append = ({'EOF':'EOF'}) > TypeError: object has read-only attributes > > >From what I read I thought one could append onto a list. When you're having trouble, eliminate the variables. You can evoke this error in the Python interpreter without any databases involved. >>> L = [1,2] >>> L.append >>> L.append = "doesn't matter what you try" Traceback (innermost last): File "", line 1, in ? TypeError: object has read-only attributes > A side question- > What I'm trying to do is reconize when I read all the data from the > list. I use 'for' loop but have found nothing in the material I read > that would indicate how to determine the end of the list. This is really a question for the FAQ, the Language Reference, or comp.lang.python. To save you some trouble, you can do either of the following (the first is a more common idiom in Python) >>> L = [1,1,2,3,5,8] >>> for fibNumber in L: ... print fibNumber, ... 1 1 2 3 5 8 >>> for idx in range(len(L)): ... print L[idx], ... 1 1 2 3 5 8 ---Tom From mrw@plexus.demon.co.uk Sat Feb 12 11:15:55 2000 From: mrw@plexus.demon.co.uk (Mike Wilson) Date: Sat, 12 Feb 2000 11:15:55 +0000 Subject: [DB-SIG] DCOracle & Oracle 8.1.5 (Linux) Message-ID: <00021211192400.11559@trident> It was hard work, but I managed to build DCOracle for Oracle 8.1.5. Problem is, it regularly goes "Segmentation fault" on pretty simple scripts like: import DCOracle dbc = DCOracle.Connect ("scott/tiger@orcl") cursor = dbc.cursor () cursor.execute ("select * from emp order by ename") row = cursor.fetchone () while row: print row [0] row = cursor.fetchone () The script prints all the rows, so it looks like it's crashing inside Oracle or DCOracle during cleanup. Has anyone had similar problems? Any idea what the solution is? ---------------------------------------------- Mike Wilson Plexus Systems Ltd, Reading Berkshire, UK mrw@plexus.demon.co.uk http://www.plexus.demon.co.uk/ From pnpayne@swissonline.ch Sun Feb 13 08:38:02 2000 From: pnpayne@swissonline.ch (Philip Payne) Date: Sun, 13 Feb 2000 09:38:02 +0100 Subject: [DB-SIG] Re: DB-SIG digest, Vol 1 #218 - 1 msg References: <20000212170017.E51461CDE9@dinsdale.python.org> Message-ID: <38A66D6A.42EF@swissonline.ch> > --------------------------------------------------------------- > > Subject: [DB-SIG] DCOracle & Oracle 8.1.5 (Linux) > Date: Sat, 12 Feb 2000 11:15:55 +0000 > From: Mike Wilson > Organization: Plexus Systems Ltd > To: db-sig@python.org > > It was hard work, but I managed to build DCOracle for Oracle > 8.1.5. Problem is, it regularly goes "Segmentation fault" on > pretty simple scripts like: > > import DCOracle > > dbc = DCOracle.Connect ("scott/tiger@orcl") > cursor = dbc.cursor () > > cursor.execute ("select * from emp order by ename") > > row = cursor.fetchone () > > while row: > print row [0] > row = cursor.fetchone () > > The script prints all the rows, so it looks like it's crashing > inside Oracle or DCOracle during cleanup. Has anyone had > similar problems? Any idea what the solution is? > > ---------------------------------------------- > Mike Wilson Plexus Systems Ltd, Reading Berkshire, UK > mrw@plexus.demon.co.uk http://www.plexus.demon.co.uk/ Mike, Try changing your make file to compile DCOracle without optimization: cc -O0 ... (that's oh zero). I was having the same behaviour on Linux until I finally remembered that OCI programs always used to have to be compiled with optimization disabled and made this change. Since then - no problems. (It's many years since I wrote an OCI program, and Oracle don't explicitly mention disabling optimization in their recent documentation, so I'd be interested to know if people had the same problem on other platforms.) Regards, Philip Payne From mrw@plexus.demon.co.uk Sun Feb 13 19:58:23 2000 From: mrw@plexus.demon.co.uk (Mike Wilson) Date: Sun, 13 Feb 2000 19:58:23 +0000 Subject: [DB-SIG] Re: DB-SIG digest, Vol 1 #218 - 1 msg In-Reply-To: <38A66D6A.42EF@swissonline.ch> References: <20000212170017.E51461CDE9@dinsdale.python.org> <38A66D6A.42EF@swissonline.ch> Message-ID: <00021319591900.21268@trident> > > I was having the same behaviour on Linux until I finally remembered that > OCI programs always used to have to be compiled with optimization > disabled and made this change. Since then - no problems. (It's many > years since I wrote an OCI program, and Oracle don't explicitly mention > disabling optimization in their recent documentation, so I'd be > interested to know if people had the same problem on other platforms.) > Yes, that seems to have done the trick -- many thanks for the tip. Pretty obscure, eh ? Regards, ---------------------------------------------- Mike Wilson Plexus Systems Ltd, Reading Berkshire, UK mrw@plexus.demon.co.uk http://www.plexus.demon.co.uk/ From battery841@excite.com Mon Feb 14 00:09:47 2000 From: battery841@excite.com (battery841@excite.com) Date: Sun, 13 Feb 2000 16:09:47 -0800 (PST) Subject: [DB-SIG] DSN question Message-ID: <28865535.950486987135.JavaMail.imail@swirly> I am writing a Python app which is using MySQL (http://starship.python.net/crew/adustman/MySQLdb.html) on a Linux box to talk to a mySQL database. A friend said I need (or at least should, I don't remember) use DSN to get the host/login/password information. I don't necessarily understand the concept of it. I am kind of new to Python and a little lost on this. My friend said DSN uses an external file to read that information. However, the MySQL docs seem like they want me to specify the information in the mysql.connect() line. How do I go about doing this? I'd be connecting to a local host. Would I put 127.0.0.1 or localhost? Can someone give me a hand? I'm lost :) Thanks a lot Kevin _______________________________________________________ Get 100% FREE Internet Access powered by Excite Visit http://freeworld.excite.com From Gstarchman@urbanbrands.com Mon Feb 14 12:02:26 2000 From: Gstarchman@urbanbrands.com (Glen Starchman) Date: Mon, 14 Feb 2000 07:02:26 -0500 Subject: [DB-SIG] DSN question Message-ID: <5AA629DE3ABFD311995600A0C996074A039150@EX_SERVER> A file DSN is only applicable if you are using ODBC. You want to do something like: mysql.connect("127.0.0.1/username/password") I do recommend, however, for both security and maintenance issues that you place the connection string in an included file like: #this is connect.py CONNECT_STRING = "127.0.0.1/username/password" #then, every file that needs to create a mysql connection would do... import connect mysql.connect(connect.CONNECT_STRING) Bear in mind that I have not used the mysql module so the syntax of the connect string might be slightly different. Check the docs! ;-) ******************************************** Glen Starchman Director Of Web Technology http://www.100percentgirls.com 917-402-0497 ******************************************** -----Original Message----- From: db-sig-admin@python.org [mailto:db-sig-admin@python.org]On Behalf Of battery841@excite.com Sent: Sunday, February 13, 2000 7:10 PM To: db-sig@python.org Subject: [DB-SIG] DSN question I am writing a Python app which is using MySQL (http://starship.python.net/crew/adustman/MySQLdb.html) on a Linux box to talk to a mySQL database. A friend said I need (or at least should, I don't remember) use DSN to get the host/login/password information. I don't necessarily understand the concept of it. I am kind of new to Python and a little lost on this. My friend said DSN uses an external file to read that information. However, the MySQL docs seem like they want me to specify the information in the mysql.connect() line. How do I go about doing this? I'd be connecting to a local host. Would I put 127.0.0.1 or localhost? Can someone give me a hand? I'm lost :) Thanks a lot Kevin _______________________________________________________ Get 100% FREE Internet Access powered by Excite Visit http://freeworld.excite.com _______________________________________________ DB-SIG maillist - DB-SIG@python.org http://www.python.org/mailman/listinfo/db-sig From adustman@comstar.net Fri Feb 18 04:05:45 2000 From: adustman@comstar.net (Andy Dustman) Date: Thu, 17 Feb 2000 23:05:45 -0500 (EST) Subject: [DB-SIG] DSN question In-Reply-To: <28865535.950486987135.JavaMail.imail@swirly> Message-ID: On Sun, 13 Feb 2000 battery841@excite.com wrote: > I am writing a Python app which is using MySQL > (http://starship.python.net/crew/adustman/MySQLdb.html) on a Linux box to > talk to a mySQL database. A friend said I need (or at least should, I don't > remember) use DSN to get the host/login/password information. I don't > necessarily understand the concept of it. I am kind of new to Python and a > little lost on this. My friend said DSN uses an external file to read that > information. However, the MySQL docs seem like they want me to specify the > information in the mysql.connect() line. How do I go about doing this? I'd > be connecting to a local host. Would I put 127.0.0.1 or localhost? Can > someone give me a hand? I'm lost :) Thanks a lot > Kevin MySQLdb doesn't use a DSN string, mainly because MySQL doesn't. MySQLdb.Connect() uses a series of keyword parameters, which all have sensible defaults. You probably will want to use db, user, passwd. You can use host (defaults to localhost), unix_socket (boolean integer), and/or port (default is 3306) (don't take these defaults as the literal truth; some of them default to NULL pointers or zeros, and the MySQL C interface interprets them as stated). There's also a client_flag; I forget what that does, but you probably don't need it. This stuff is documented, honest. Also check the MySQL C API docs for mysql_connect(). -- andy dustman | programmer/analyst | comstar.net, inc. telephone: 770.485.6025 / 706.549.7689 | icq: 32922760 | pgp: 0xc72f3f1d "Therefore, sweet knights, if you may doubt your strength or courage, come no further, for death awaits you all, with nasty, big, pointy teeth!" From adustman@comstar.net Fri Feb 18 04:16:46 2000 From: adustman@comstar.net (Andy Dustman) Date: Thu, 17 Feb 2000 23:16:46 -0500 (EST) Subject: [DB-SIG] [ANNOUNCEMENT] MySQLdb-0.1.2 now available Message-ID: -----BEGIN PGP SIGNED MESSAGE----- After a long testing period during which nobody complained :-/, I have released MySQLdb-0.1.2, which is a DB API v2.0 compliant interface to MySQL-3.22. You can find it at: http://starship.python.net/crew/adustman/MySQLdb.html New features: - -- There is now a literal conversion dictionary which can be used to control the conversion of Python types into MySQL literals (default is to treat as a string). This results in better literal handling for numbers and user-defined objects. - -- Some of the MySQL-specific functions are now accessible directly through the MySQL Connection object. These are quite non-portable to other databases, but if you need 'em, they're now easier to get to. Zope smokers: The ZMySQLDA patch is unchanged. In fact, none of the above features affect you in the slightest, so no rush to upgrade. :) Due to time constraints, I have not been able to keep up at all with the Zope mailing list, so if there are any issues with ZMySQLDA and MySQLdb, please cc: me or write me directly. - -- andy dustman | programmer/analyst | comstar.net, inc. telephone: 770.485.6025 / 706.549.7689 | icq: 32922760 | pgp: 0xc72f3f1d "Therefore, sweet knights, if you may doubt your strength or courage, come no further, for death awaits you all, with nasty, big, pointy teeth!" -----BEGIN PGP SIGNATURE----- Version: 2.6.3a Charset: noconv iQEPAwUBOKzHrxOPBZTHLz8dAQHU2wfPQGZPHRTgjKY34+/XPyYCDpH+Z2V3y/MJ uTBXTDRch9sKYMn5mvE0CuSvpZYY+zpwwMYhShRVz/EVlTEZXqQgA9fIyvQ5Rpn/ MDrYaAX968T07NYjeVfanvYzQkUrsuBfLbzFujk3vpooojP/lFSWanStKXJ2/5+z VWoUJLYX2q31dxe26Lvx2+fpLdqpn4RAkzlXLw5CzM+04s61etNbdDtWPRb386fY Zxjhw8FJg6B0zaaZ4aB/skhTGUjJ/yqzqDeq5id5Pldk2zBOmtvHXmxB1f+z56gO F8iZMNTLoP9X4cPyUWdCS2PZdbnr7QBBsIxe6lC4vcVH9Q== =ehLh -----END PGP SIGNATURE----- From sgendler@impossible.com Fri Feb 18 23:39:59 2000 From: sgendler@impossible.com (Sam Gendler) Date: Fri, 18 Feb 2000 15:39:59 -0800 Subject: [DB-SIG] Re: [Zope] [ANNOUNCEMENT] MySQLdb-0.1.2 now available References: Message-ID: <38ADD84F.3073237@impossible.com> > > Zope smokers: The ZMySQLDA patch is unchanged. In fact, none of the above > features affect you in the slightest, so no rush to upgrade. :) Due to > time constraints, I have not been able to keep up at all with the Zope > mailing list, so if there are any issues with ZMySQLDA and MySQLdb, please > cc: me or write me directly. There was a thread awhile back about having the DA return the value of the auto_increment fields (if available) as the result set to an insert query. I suspect this is more in the code of the zope DA than the python adaptor, but if you know your way around the zope DA, perhaps you could create a patch. I made a quick attempt a while back, and failed miserably. --sam From adustman@comstar.net Sun Feb 20 16:09:18 2000 From: adustman@comstar.net (Andy Dustman) Date: Sun, 20 Feb 2000 11:09:18 -0500 (EST) Subject: [DB-SIG] Re: [Zope] [ANNOUNCEMENT] MySQLdb-0.1.2 now available In-Reply-To: <38ADD84F.3073237@impossible.com> Message-ID: On Fri, 18 Feb 2000, Sam Gendler wrote: > There was a thread awhile back about having the DA return the value of > the auto_increment fields (if available) as the result set to an insert > query. I suspect this is more in the code of the zope DA than the > python adaptor, but if you know your way around the zope DA, perhaps you > could create a patch. I made a quick attempt a while back, and failed > miserably. Since the patched ZMySQLDA only uses _mysql (the C interface part used by MySQLdb), you should be able to get the info you want from db.insert_id(), where db is the database connection object. Note that this will return a long integer. I don't know how you would incorporate this into ZMySQLDA, however. (The above method now works on MySQLdb connection objects as well, as of 0.1.2.) -- andy dustman | programmer/analyst | comstar.net, inc. telephone: 770.485.6025 / 706.549.7689 | icq: 32922760 | pgp: 0xc72f3f1d "Therefore, sweet knights, if you may doubt your strength or courage, come no further, for death awaits you all, with nasty, big, pointy teeth!" From michael.guidry@trigenttech.com Fri Feb 18 21:45:26 2000 From: michael.guidry@trigenttech.com (Michael Guidry) Date: Fri, 18 Feb 2000 15:45:26 -0600 Subject: [DB-SIG] executemany Message-ID: I'm new at python but like what I see. Just starting to devour M. Hammonds new book Programming on Win32 - fairly good read so far. Running on NT using MS SQL database. I think I've installed the mxODBC and mxDateTime code correctly and renamed the original odbc.pyd. I try to cursor.executemany but get AttributeError: executemany. Here's the codesnip. Any other WIN32 MS SQL folks out there with some hints. Thanks in advance. import ODBC.Windows conn = ODBC.Windows.Connect('Music') crsr = conn.cursor() stmt = "INSERT INTO Country (name1, name2) VALUES (?,?)" crsr.executemany(stmt, ('Tom','Jones') ('George','Smith') ] ) Traceback (innermost last): File "", line 2, in ? AttributeError: executemany Michael Guidry, PMP From petrilli@amber.org Tue Feb 22 14:16:41 2000 From: petrilli@amber.org (Christopher Petrilli) Date: Tue, 22 Feb 2000 09:16:41 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <36D135DF.ABBEC61B@embl-heidelberg.de>; from John.Duperon@EMBL-Heidelberg.de on Mon, Feb 22, 1999 at 11:47:59AM +0100 References: <36D135DF.ABBEC61B@embl-heidelberg.de> Message-ID: <20000222091641.B14316@trump.amber.org> John Duperon [John.Duperon@EMBL-Heidelberg.de] wrote: > Hello, > > my colleagues and I will be creating online databases to > hold our scientific data. Some of these projects will be > more complex than others, and thus we would like to use > MySQL for most of them and Oracle where data integrity is > essential. With this in mind, we are trying to decide > whether to use Perl or Python for connecting our databases > to the web. Do you have any idea whether the Python > database API is the same for Oracle and MySQL? We'd like to > be able to write code that will work with either database, > requiring only the change of the database module, as can be > done with Perl's DBI. Since I manage teh Database stuff for Zope (which you might consider as well...), and work on the DCOracle adapter, here's my take. * DB-API is pretty consistent * SQL is NOT What this means is that moving between MySQL and Oracle often is more an issue of the SQL you use than anything else, especially given the difference in expressiveness (lack of subselects, etc in MySQL). However, if you're careful, yo ucan write stuff that will work between both. Chris -- | Christopher Petrilli | petrilli@amber.org From Cary Collett Tue Feb 22 14:42:42 2000 From: Cary Collett (Cary Collett) Date: Tue, 22 Feb 2000 09:42:42 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <20000222091641.B14316@trump.amber.org>; from petrilli@amber.org on Tue, Feb 22, 2000 at 09:16:41AM -0500 References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> Message-ID: <20000222094242.O3463@ratatosk.org> I'll second Christopher's post. The DBI-API interfaces are nice and consistent. The differences in Oracle's and MySQL's SQL dialects are much more significant and will be your sticking point in any migration between the two. Personally, I like MySQL's SQL better. There are some very handy features that Oracle just doesn't have. (This is all as of Oracle 7, I haven't used 8.) Cary Thus spake Christopher Petrilli (petrilli@amber.org): > John Duperon [John.Duperon@EMBL-Heidelberg.de] wrote: > > Hello, > > > > whether to use Perl or Python for connecting our databases > > to the web. Do you have any idea whether the Python > > database API is the same for Oracle and MySQL? We'd like to > > be able to write code that will work with either database, > > requiring only the change of the database module, as can be > > done with Perl's DBI. > > Since I manage teh Database stuff for Zope (which you might consider > as well...), and work on the DCOracle adapter, here's my take. > > * DB-API is pretty consistent > > * SQL is NOT > > What this means is that moving between MySQL and Oracle often is more > an issue of the SQL you use than anything else, especially given the > difference in expressiveness (lack of subselects, etc in MySQL). However, > if you're careful, yo ucan write stuff that will work between both. > > Chris > -- > | Christopher Petrilli > | petrilli@amber.org > > _______________________________________________ > DB-SIG maillist - DB-SIG@python.org > http://www.python.org/mailman/listinfo/db-sig > > -- Cary Collett cary@ratatosk.org http://ratatosk.org/~cary "To me boxing is like ballet, except that there's no music, no choreography, and the dancers hit eachother." -- Jack Handy From hniksic@iskon.hr Tue Feb 22 15:49:31 2000 From: hniksic@iskon.hr (Hrvoje Niksic) Date: 22 Feb 2000 16:49:31 +0100 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: Cary Collett's message of "Tue, 22 Feb 2000 09:42:42 -0500" References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> Message-ID: <9t9n1otz184.fsf@mraz.iskon.hr> Cary Collett writes: > I'll second Christopher's post. The DBI-API interfaces are nice and > consistent. The differences in Oracle's and MySQL's SQL dialects are > much more significant and will be your sticking point in any > migration between the two. Personally, I like MySQL's SQL > better. There are some very handy features that Oracle just doesn't > have. (This is all as of Oracle 7, I haven't used 8.) Out of curiosity, can you name some? I've mostly heard about the features MySQL does *not* have, like transactions, subselects, foreign keys, triggers, stored procedures, etc. From Cary Collett Tue Feb 22 19:25:28 2000 From: Cary Collett (Cary Collett) Date: Tue, 22 Feb 2000 14:25:28 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <9t9n1otz184.fsf@mraz.iskon.hr>; from hniksic@iskon.hr on Tue, Feb 22, 2000 at 04:49:31PM +0100 References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> <9t9n1otz184.fsf@mraz.iskon.hr> Message-ID: <20000222142528.P3463@ratatosk.org> For me LIMIT is the big one: SELECT ... LIMIT 4 (return the first 4 results) SELECT ... LIMIT 3,5 (return the 4th through 9th rows) Cary Thus spake Hrvoje Niksic (hniksic@iskon.hr): > Cary Collett writes: > > > I'll second Christopher's post. The DBI-API interfaces are nice and > > consistent. The differences in Oracle's and MySQL's SQL dialects are > > much more significant and will be your sticking point in any > > migration between the two. Personally, I like MySQL's SQL > > better. There are some very handy features that Oracle just doesn't > > have. (This is all as of Oracle 7, I haven't used 8.) > > Out of curiosity, can you name some? > > I've mostly heard about the features MySQL does *not* have, like > transactions, subselects, foreign keys, triggers, stored procedures, > etc. > > _______________________________________________ > DB-SIG maillist - DB-SIG@python.org > http://www.python.org/mailman/listinfo/db-sig > > -- Cary Collett cary@ratatosk.org http://ratatosk.org/~cary "To me boxing is like ballet, except that there's no music, no choreography, and the dancers hit eachother." -- Jack Handy From petrilli@amber.org Tue Feb 22 22:01:23 2000 From: petrilli@amber.org (Christopher Petrilli) Date: Tue, 22 Feb 2000 17:01:23 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <20000222142528.P3463@ratatosk.org>; from cary@ratatosk.org on Tue, Feb 22, 2000 at 02:25:28PM -0500 References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> <9t9n1otz184.fsf@mraz.iskon.hr> <20000222142528.P3463@ratatosk.org> Message-ID: <20000222170123.A15232@trump.amber.org> Cary Collett [cary@ratatosk.org] wrote: > > For me LIMIT is the big one: > > SELECT ... LIMIT 4 (return the first 4 results) > SELECT ... LIMIT 3,5 (return the 4th through 9th rows) Correct behaviour of scrollable cursors will get you this in a MUCH more advanced form :-) Chris -- | Christopher Petrilli | petrilli@amber.org From bradh@mediaone.net Wed Feb 23 00:20:05 2000 From: bradh@mediaone.net (Brad Howes) Date: 22 Feb 2000 19:20:05 -0500 Subject: [DB-SIG] New Oracle8i OCI Module Message-ID: OK, I've got an Oracle8 OCI module available for people to muck with. So far, its been tested and works fine on Solaris 2.6, Windows NT, and RedHat 6.1. What's the best way to submit this for people look at, critique? Brad -- "Were people this stupid before TV?" -- _White Noise_ by Don DeLillo From tbryan@starship.python.net Wed Feb 23 04:10:50 2000 From: tbryan@starship.python.net (Tom Bryan) Date: Tue, 22 Feb 2000 23:10:50 -0500 (EST) Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <20000222091641.B14316@trump.amber.org> Message-ID: On Tue, 22 Feb 2000, Christopher Petrilli wrote: > John Duperon [John.Duperon@EMBL-Heidelberg.de] wrote: > > > > my colleagues and I will be creating online databases to > > hold our scientific data. Some of these projects will be > > more complex than others, and thus we would like to use > > MySQL for most of them and Oracle where data integrity is > > essential. With this in mind, we are trying to decide > > whether to use Perl or Python for connecting our databases > > to the web. Do you have any idea whether the Python > > database API is the same for Oracle and MySQL? We'd like to > > be able to write code that will work with either database, > > requiring only the change of the database module, as can be > > done with Perl's DBI. [...snip...] > What this means is that moving between MySQL and Oracle often is more > an issue of the SQL you use than anything else, especially given the > difference in expressiveness (lack of subselects, etc in MySQL). However, > if you're careful, yo ucan write stuff that will work between both. Another consideration is where you want your flexibility. It may be wise to simply hide the SQL from almost all of your code in some sort of data access classes. Those classes know how and where to find the data (MySQL DB, Oracle DB, flat file, somewhere out on the web, etc.), and other objects who need to get data just instantiate one or more data access objects to get the data. Especially in light of what Christopher says here, you may want to consider an architecture following something like the adapter pattern so that you can easily change the "back end" without affecting any of your code other than the data access (adapter) class. If you decide to follow this advice, then I would recommend Python since it's straightforward to work with classes and design nice architectures in Python. I have found such tasks to be more...um...challenging when working with Perl. That is, I always get the impression that I'm fighting Perl when I try to do such things. When I work with Python, it's difficult for me to write anything without eventually finding that I've factored behaviour out into a handful of classes in one or more modules. I found that Python in general improves the ease and frequency of code reuse (compared to Perl). Don't forget that you'll have to maintain whatever you write. :-) Then again, perhaps I'm mistaking the scope and requirements of your applications and your relative experience with Perl and Python. ---Tom From petrilli@amber.org Wed Feb 23 04:31:28 2000 From: petrilli@amber.org (Christopher Petrilli) Date: Tue, 22 Feb 2000 23:31:28 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: ; from tbryan@starship.python.net on Tue, Feb 22, 2000 at 11:10:50PM -0500 References: <20000222091641.B14316@trump.amber.org> Message-ID: <20000222233128.A16111@trump.amber.org> Tom Bryan [tbryan@starship.python.net] wrote: > > Another consideration is where you want your flexibility. It may be wise > to simply hide the SQL from almost all of your code in some sort of data > access classes. Those classes know how and where to find the data (MySQL > DB, Oracle DB, flat file, somewhere out on the web, etc.), and other > objects who need to get data just instantiate one or more data access > objects to get the data. Especially in light of what Christopher says > here, you may want to consider an architecture following something like > the adapter pattern so that you can easily change the "back end" without > affecting any of your code other than the data access (adapter) class. This is the architecture that I push with Zope (and also one that Apple has pushed in Enterprise Objects). Unfortunately, often people skip the multi-tier architecture because it's not familiar, or they feel that their project won't ever change... often a misled idea :-) I think is important... unfortunately SQLMethods in Zope aren't easily removed from the rest of the framework, but this is what they provide. Chris -- | Christopher Petrilli | petrilli@amber.org From alex@ank-sia.com Wed Feb 23 09:19:12 2000 From: alex@ank-sia.com (alexander smishlajev) Date: Wed, 23 Feb 2000 11:19:12 +0200 Subject: [DB-SIG] Creating portable Python database code References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> Message-ID: <38B3A610.1DF12EC1@turnhere.com> Cary Collett wrote: > > I'll second Christopher's post. The DBI-API interfaces are > nice and consistent. The differences in Oracle's and MySQL's > SQL dialects are much more significant and will be your > sticking point in any migration between the two. provided that i am using only basic sql features available on either platform, can someone show me an examle of writing python code that can work with _any_ Python DBI (both 1.0 and 2.0) compliant module? select and insert or update with numeric, varchar and datetime columns is sufficient; long and raw columns (AKA LOBs and BLOBs) -- better. in Perl DBI, moving to different database is as easy as changing connection setting. AFAIU the same is true for Zope. but doing this in Python seems to be much more tricky. am i missing something important? best wishes, alex. From petrilli@amber.org Wed Feb 23 20:06:47 2000 From: petrilli@amber.org (Christopher Petrilli) Date: Wed, 23 Feb 2000 15:06:47 -0500 Subject: [DB-SIG] Creating portable Python database code In-Reply-To: <38B3A610.1DF12EC1@turnhere.com>; from als@turnhere.com on Wed, Feb 23, 2000 at 11:19:12AM +0200 References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> <38B3A610.1DF12EC1@turnhere.com> Message-ID: <20000223150647.A20300@trump.amber.org> alexander smishlajev [als@turnhere.com] wrote: > in Perl DBI, moving to different database is as easy as changing > connection setting. AFAIU the same is true for Zope. but doing > this in Python seems to be much more tricky. am i missing > something important? It's not more complex in Python *IF* you keep your SQL compatible with whatever subset works on all platforms. You just have to import a different module and instantiate the class... e.g.: import DCOracle conn = DCOracle.Connection(...) to... import Sybase conn = Sybase.Connection(...) Then the 'conn' object should be have the same. Chris -- | Christopher Petrilli | petrilli@amber.org From alex@ank-sia.com Thu Feb 24 12:44:00 2000 From: alex@ank-sia.com (alexander smishlajev) Date: Thu, 24 Feb 2000 14:44:00 +0200 Subject: [DB-SIG] Creating portable Python database code References: <36D135DF.ABBEC61B@embl-heidelberg.de> <20000222091641.B14316@trump.amber.org> <20000222094242.O3463@ratatosk.org> <38B3A610.1DF12EC1@turnhere.com> <20000223150647.A20300@trump.amber.org> Message-ID: <38B52790.ED7F7240@turnhere.com> Christopher Petrilli wrote: > > > in Perl DBI, moving to different database is as easy as changing > > connection setting. AFAIU the same is true for Zope. but doing > > this in Python seems to be much more tricky. am i missing > > something important? > > It's not more complex in Python *IF* you keep your SQL compatible with > whatever subset works on all platforms. You just have to import a different > module and instantiate the class... e.g.: > > import DCOracle > > conn = DCOracle.Connection(...) > > to... > > import Sybase > > conn = Sybase.Connection(...) ok. i can import a module and create connection. my problem is with dates: i failed to find a consistent way to handle datetime (or whatever this data type is called) values. best wishes, alex. From mal@lemburg.com Mon Feb 28 09:03:03 2000 From: mal@lemburg.com (M.-A. Lemburg) Date: Mon, 28 Feb 2000 10:03:03 +0100 Subject: [DB-SIG] executemany References: Message-ID: <38BA39C7.838C6980@lemburg.com> Michael Guidry wrote: > > I'm new at python but like what I see. > Just starting to devour M. Hammonds new book Programming on Win32 - fairly > good read so far. Running on NT using MS SQL database. > I think I've installed the mxODBC and mxDateTime code correctly and renamed > the original odbc.pyd. I try to cursor.executemany but get AttributeError: > executemany. Here's the codesnip. Any other WIN32 MS SQL folks out there > with some hints. Thanks in advance. > > import ODBC.Windows > conn = ODBC.Windows.Connect('Music') > crsr = conn.cursor() > stmt = "INSERT INTO Country (name1, name2) VALUES (?,?)" > crsr.executemany(stmt, > ('Tom','Jones') > ('George','Smith') > ] > ) > > Traceback (innermost last): > File "", line 2, in ? > AttributeError: executemany .executemany() is not yet supported by mxODBC. You can use .execute() with the same arguments though -- DB API 1.0 defines more or less the same semantics for .execute() and .executemany() and mxODBC still supports most of the DB API 1.0 features. -- Marc-Andre Lemburg ______________________________________________________________________ Business: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/