Connecting to ODBC Databases from Python with pyodbcpyodbc is an open source Python module that provides access to ODBC databases. pyodbc implements the Python DB API 2.0 specification. The Python DB API defines a database-neutral interface to data stored in relational databases. Python DB was designed to allow conformant modules to provide a consistent interface to different database products. This helps developers to write Python applications that are portable across databases. pyodbc is a Python DB conformant module for ODBC databases. This tutorial shows how to use pyodbc with an ODBC driver, which you can download from this site. You can then connect Python on Linux and Unix to remote database such as Microsoft SQL Server, Oracle, Microsoft Access, Sybase ASE and InterBase. pyodbc and LinuxEasysoft ODBC drivers have been tested with pyodbc 2.0+ on RedHat, Ubuntu (Edgy Eft, Feisty Fawn, Gutsy Gibbon and Hardy Heron) and Debian, but should work with any recent Linux distribution (CentOS, Fedora, Mandrake, SUSE and so on). pyodbc PrerequisitesPythonThe pyodbc module requires Python 2.4 or greater (see README.txt, which is included with the pyodbc distribution). To build pyodbc, you need the Python libraries and header files, and a C++ compiler. When testing on RedHat, we used Python 2.5.1, the python-devel package and the gcc-c++ package. On Ubuntu, we used Python 2.5.1, the python-dev package and the g++ package. ODBC DriverTo use pyodbc, you need to install an ODBC driver on the machine Python where is installed:
Installing pyodbcOn Unix and Linux platforms, you need to download the pyodbc source distribution and build it against an ODBC driver manager. These instructions show how to build pyodbc against the unixODBC driver manager supplied with an Easysoft ODBC driver. We recommend that you use the driver manager distributed with the driver because this is the version of unixODBC that we test the driver with.
Testing pyodbcThe pyodbc distribution includes two test suites: The Python DB API 2.0 TestsThe Python DB API 2.0 test suite was written to allow Python DB developers to verify their driver’s DB API conformance. As the tests access and manipulate database tables, they provide another way to test pyodbc against your ODBC driver and database. We therefore recommend that you run them. To do this:
The pyodbc tests allow you to ensure that an ODBC driver is compatible with pyodbc. Note
Some tests use data types and SQL syntax that are supported by SQL
Server but not other databases. The test suite is most relevant to SQL
Server therefore. The test suite does skip some tests based on data
type information reported by the ODBC driver. However, some tests are
still run even though the driver has reported that the prerequisite
data type is not available. (To see which tests pyodbctest skips,
include the When
we tested pyodbc, Easysoft ODBC drivers passed all tests that the
target database was capable of passing. For example, when we ran the
pyodbc test suite against Oracle Database XE, # Recreate existing procedure using syntax that Oracle supports. self.cursor.execute(""" create or replace procedure pyodbctest (var1 IN OUT VARCHAR2) is begin select s into var1 from t1; end; """) self.cnxn.commit() # Call the procedure, using the more portable ODBC escape sequence. # The ODBC driver for the target database will replaces this escape # sequence with the appropriate DBMS-specific syntax for calling # procedures. This method for calling procedures will therefore work # for both Oracle and SQL Server. Note that pyodbc does not # currently support the DB method callproc(). self.cursor.execute("{call pyodbctest(?)}", ('testing')) In addition to these issues, please note the following before running the tests:
Running the Test SuiteTo run the tests, cd into the directory created by unzipping the pyodbc distribution file and type: $ python pyodbctests.py DSN=data_source where To run an individual test rather than all tests, include pyodbc Example: Connecting Python to Microsoft SQL ServerIn the example session shown here, we used pyodbc with the SQL Server ODBC driver to connect Python to a SQL Server Express database. The driver can also be used to access other SQL Server 2005 editions from Python on Linux and Unix. (As well as earlier versions of the database such as SQL Server 2000 and SQL Server 7.0.) In the $ python Python 2.5.1 (r251:54863, Jan 25 2008, 16:14:49) [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pyodbc >>> cnxn = pyodbc.connect("DSN=MSSQL-PYTHON") >>> cursor = cnxn.cursor() >>> cursor.tables() >>> rows = cursor.fetchall() >>> for row in rows: ... print row.table_name ... Categories CustomerCustomerDemo CustomerDemographics Customers Employees EmployeeTerritories . . . >>> exit() pyodbc Example: Connecting Python to OracleTo connect to a different DBMS, the only change to the Python code (shown in the previous section) that you need to make is the data source name. For example: >>> cnxn = pyodbc.connect("DSN=ORACLE-PYTHON")
Appendix A: Resources
|
|
來自: xiaoqdu > 《開發(fā)資料》