乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Connecting to ODBC Databases from Python with pyodbc

       xiaoqdu 2008-12-08

      Connecting to ODBC Databases from Python with pyodbc

      pyodbc 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 Linux

      Easysoft 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 Prerequisites

      Python

      The 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 Driver

      To use pyodbc, you need to install an ODBC driver on the machine Python where is installed:

      1. Download the ODBC driver for your Python and database platform. (Registration required.)

        For example, if want to access SQL Server from Python, download the Easysoft ODBC-SQL Server Driver for your Python platform. If the SQL Server ODBC driver is not currently available for your platform, check the list of ODBC-ODBC Bridge client platforms. The ODBC-ODBC Bridge is an alternative SQL Server solution from Easysoft, which you can download from this site.

      2. Install and license the ODBC driver on the machine where Python is installed.

        For installation instructions, see the ODBC driver documentation. Refer to the documentation to see which environment variables you need to set (LD_LIBRARY_PATH, LIBPATH, LD_RUN_PATH, SHLIB_PATH or ORACLE_HOME depending on the driver, platform and linker).

      3. Create a ODBC data source in /etc/odbc.ini that connects to the database you want to access. For example, this SQL Server ODBC data source connects to a SQL Server Express instance that serves the Northwind database:
        [MSSQL-PYTHON]
            Driver                  = Easysoft ODBC-SQL Server Driver
            Server                  = my_machine\SQLEXPRESS
            User                    = my_domain\my_user
            Password                = my_password
            # If the database you want to connect to is the default
            # for the SQL Server login, omit this attribute
            Database                = Northwind
      4. Use isql to test the new data source. For example:
        cd /usr/local/easysoft/unixODBC/bin
            ./isql -v MSSQL-PYTHON

        At the prompt, type "help" to display a list of tables. To exit, press return in an empty prompt line.

      Installing pyodbc

      On 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.

      1. Download the platform-independent source distribution from the pyodbc web site.
      2. Copy the distribution file to your Python machine, unzip and cd into the directory created by unzipping the file. For example:
        $ unzip pyodbc-2.0.52.zip
            $ cd pyodbc-2.0.52
      3. To build pyodbc against unixODBC, you need to tell the compiler and linker where to find the unixODBC include files and libraries. To do this, open setup.py in a text editor and find these lines.
        extra_compile_args = None
            extra_link_args    = None
            

        Edit the lines so that they look like this:

        extra_compile_args = ['-I/usr/local/easysoft/unixODBC/include']
            extra_link_args    = ['-L/usr/local/easysoft/unixODBC/lib']
            

        Note In pyodbc versions earlier than 2.0.52, setup.py was named setup.PY.

      4. Build pyodbc:
        $ python setup.py build

        Note If you need to rebuild pyodbc, first remove the build directory tree by using rm -r build rather than python setup.py clean. When testing with pyodbc 2.0.52, we found that the clean command failed to remove pyodbc.so. As a consequence, running python setup.py build failed to rebuild pyodbc.so.

      5. As root, install pyodbc:
        # python setup.py install

      Testing pyodbc

      The pyodbc distribution includes two test suites:

      The Python DB API 2.0 Tests

      The 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:

      1. cd into the directory created by unzipping the pyodbc distribution file.
      2. Open setup.cfg in a text editor.
      3. In the [apitest] section, specify your ODBC data source in the connection-string value. For example:
        [apitest]
            connection-string=DSN=MSSQL-PYTHON
      4. Run the tests:
        $ python setup.py -v apitest

      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 -d option when running the tests.)

      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, test_sqlserver_callproc failed because it uses SQL Server specific syntax to create and execute a stored procedure. If the test is modified to use SQL syntax that Oracle supports, the test succeeds. For example:

      # 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:

      TestNotesSolution
      test_image

      test_long_binary

      Before running these tests, make sure that you have pyodbc 2.0.52 or later. When testing pyodbc with the SQL Server ODBC driver, we submitted a pyodbc patch, which fixes a problem that affects these tests. (See bug report [ 1872275 ] data at exec problem.) The patch was merged into the 2.0.52 release. If necessary, upgrade pyodbc to 2.0.52 or later.

      Running the Test Suite

      To run the tests, cd into the directory created by unzipping the pyodbc distribution file and type:

      $ python pyodbctests.py DSN=data_source

      where data_source is the name of your ODBC data source. The pyodbc tests attempt to create tables and procedures and insert and retrieve data. Your data source therefore needs to connect to a database in which these actions are permitted.

      To run an individual test rather than all tests, include -t test_name before the DSN setting. (Type python pyodbctests.py --help to display all test suite options.)

      pyodbc Example: Connecting Python to Microsoft SQL Server

      In 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 pyodbc.connect() call, replace MSSQL-PYTHON with the name of your SQL Server ODBC driver data source.

      $ 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 Oracle

      To 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")
      

      ORACLE-PYTHON is an Oracle ODBC driver data source that we used with pyodbc to connect Python to an Oracle database.

      Appendix A: Resources

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多