Skip to main content

Use JDBC database drivers from Python 2/3 or Jython with a DB-API.

Project description

https://img.shields.io/travis/baztian/jaydebeapi/master.svg https://img.shields.io/coveralls/baztian/jaydebeapi/master.svg https://img.shields.io/badge/python-2.7,_3.5,_3.6-blue.svg https://img.shields.io/badge/jython-2.7.2-blue.svg https://img.shields.io/github/tag/baztian/jaydebeapi.svg https://img.shields.io/pypi/dm/JayDeBeApi.svg

The JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.

It works on ordinary Python (cPython) using the JPype Java integration or on Jython to make use of the Java JDBC driver.

In contrast to zxJDBC from the Jython project JayDeBeApi let’s you access a database with Jython AND Python with only minor code modifications. JayDeBeApi’s future goal is to provide a unique and fast interface to different types of JDBC-Drivers through a flexible plug-in mechanism.

Install

You can get and install JayDeBeApi with pip

$ pip install JayDeBeApi

If you want to install JayDeBeApi in Jython make sure to have pip or EasyInstall available for it.

Or you can get a copy of the source by cloning from the JayDeBeApi github project and install with

$ python setup.py install

or if you are using Jython use

$ jython setup.py install

It has been tested with Jython 2.7.2.

If you are using cPython ensure that you have installed JPype properly. It has been tested with JPype1 0.6.3 and 0.7.5 for Python 3 and with JPype1 0.6.3 and 0.7.0 for Python 2.7. Older JPype installations may cause problems.

Usage

Basically you just import the jaydebeapi Python module and execute the connect method. This gives you a DB-API conform connection to the database.

The first argument to connect is the name of the Java driver class. The second argument is a string with the JDBC connection URL. Third you can optionally supply a sequence consisting of user and password or alternatively a dictionary containing arguments that are internally passed as properties to the Java DriverManager.getConnection method. See the Javadoc of DriverManager class for details.

The next parameter to connect is optional as well and specifies the jar-Files of the driver if your classpath isn’t set up sufficiently yet. The classpath set in CLASSPATH environment variable will be honored. See the documentation of your Java runtime environment.

Here is an example:

>>> import jaydebeapi
>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver",
...                           "jdbc:hsqldb:mem:.",
...                           ["SA", ""],
...                           "/path/to/hsqldb.jar",)
>>> curs = conn.cursor()
>>> curs.execute('create table CUSTOMER'
...                '("CUST_ID" INTEGER not null,'
...                ' "NAME" VARCHAR(50) not null,'
...                ' primary key ("CUST_ID"))'
...             )
>>> curs.execute("insert into CUSTOMER values (1, 'John')")
>>> curs.execute("select * from CUSTOMER")
>>> curs.fetchall()
[(1, u'John')]
>>> curs.close()
>>> conn.close()

An alternative way to establish connection using connection properties:

>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver",
...                           "jdbc:hsqldb:mem:.",
...                           {'user': "SA", 'password': "",
...                            'other_property': "foobar"},
...                           "/path/to/hsqldb.jar",)

If you’re having trouble getting this work check if your JAVA_HOME environmentvariable is set correctly. For example I have to set it on my Ubuntu machine like this

$ JAVA_HOME=/usr/lib/jvm/java-8-openjdk python

Supported databases

In theory every database with a suitable JDBC driver should work. It is confirmed to work with the following databases:

  • SQLite

  • Hypersonic SQL (HSQLDB)

  • IBM DB2

  • IBM DB2 for mainframes

  • Oracle

  • Teradata DB

  • Netezza

  • Mimer DB

  • Microsoft SQL Server

  • MySQL

  • PostgreSQL

  • many more…

Contributing

Please submit bugs and patches. All contributors will be acknowledged. Thanks!

License

JayDeBeApi is released under the GNU Lesser General Public license (LGPL). See the file COPYING and COPYING.LESSER in the distribution for details.

Changelog

  • Next version - unreleased

  • 1.2.1 - 2020-05-27

    • Increased thread safety. Should resolve some of the No suitable driver found errors. (thanks to @thealmightygrant)

  • 1.2.0 - 2020-05-22

    • Added compatibility to JPype1 0.7.2+ (thanks to @dpd)

    • Support with statement (thanks to @Szczepanov)

  • 1.1.2 - 2019-09-02

    • Added compatibility to JPype1 0.7 (thanks to @Iverian, @Thrameos)

    • Dropped python 2.6 support

    • Fix build working with newer Maven versions

    • Accidently force-pushed to master branch. Sorry for that.

  • 1.1.1 - 2017-03-21

    • Don’t fail on dates before 1900 on Python < 3.

  • 1.1.0 - 2017-03-19

    • Support BIT and TINYINT type mappings (thanks @Mokubyow for reporting the issue).

  • 1.0.0 - 2017-01-10

    • Allow for db properties to be passed to the connect method. Probably incompatible to code based on previous versions. See documentation of the connect method. (Thanks @testlnord for you efforts and the patience.)

    • New major version due to possible api incompatibility.

  • 0.2.0 - 2015-04-26

    • Python 3 support (requires JPype1 >= 0.6.0).

  • 0.1.6 - 2015-04-10

    • Fix Jython handling of Java exceptions that don’t subclass python Exception

    • Enrich exceptions with message from java SQLExceptions

    • Be more specific about DB API exceptions: Distinguish DatabaseError and InterfaceError.

    • Fix typo LONGNARCHAR vs LONGVARCHAR (thanks @datdo for reporting #4)

  • 0.1.5 - 2015-03-02

    • Add version number to module.

    • Improve robustness of java to python type conversion.

    • Support Time type.

    • Add DB-API compliant exception handling.

    • Minor documentation improvements.

    • Some development related changes (Host project at github, use Travis CI, use JPype1 for tests).

  • 0.1.4 - 2013-10-29

    • More convenient way to setup Java classpath. Important note check the changes to the connect method and adapt your code.

    • Honor CLASSPATH if used in JPype mode.

    • Set .rowcount properly.

    • Changed signature of .setoutputsize() to be DB-API compliant.

  • 0.1.3 - 2011-01-27

    • Fixed DB-API violation: Use curs.execute('foo ?', (bar, baz)) instead of curs.execute('foo ?', bar, baz).

    • Free resources after executemany call.

    • Improved type handling. Initial support for BLOB columns.

  • 0.1.2 - 2011-01-25

    • easy_install JayDeBeApi should really work.

  • 0.1.1 - 2010-12-12

    • Fixed bug #688290 “NULL values with converters error on fetch”.

    • Fixed bug #684909 “Selecting ROWIDs errors out on fetch”.

  • 0.1 - 2010-08-10

    • Initial release.

To do

  • Extract Java calls to separate Java methods to increase performance.

  • Check if https://code.launchpad.net/dbapi-compliance can help making JayDeBeApi more DB-API compliant.

  • Test it on different databases and provide a flexible db specific pluign mechanism.

  • SQLAlchemy modules (separate project)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

JayDeBeApi-1.2.1.tar.gz (32.1 kB view details)

Uploaded Source

Built Distributions

JayDeBeApi-1.2.1-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

JayDeBeApi-1.2.1-py2-none-any.whl (26.0 kB view details)

Uploaded Python 2

File details

Details for the file JayDeBeApi-1.2.1.tar.gz.

File metadata

  • Download URL: JayDeBeApi-1.2.1.tar.gz
  • Upload date:
  • Size: 32.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9

File hashes

Hashes for JayDeBeApi-1.2.1.tar.gz
Algorithm Hash digest
SHA256 6b81e5e7f95556dfd2c9586f13d11966a759a73906aa59b036491e381e725c2d
MD5 409c6b7aa7c6b92785bb06e0951e5325
BLAKE2b-256 7d0d35e605b1f5b8ae259826771619b02062b023609590ccd46fb4d8a6af0366

See more details on using hashes here.

Provenance

File details

Details for the file JayDeBeApi-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: JayDeBeApi-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.9

File hashes

Hashes for JayDeBeApi-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3095f5f39a31b6db656410ef0a6a9da88ac83171eb414656b06133075976307
MD5 d2d3d0e41bfdcd920195cfd8cdb93823
BLAKE2b-256 063c86a96db2403caeef2b19b9fbaee276fd5ab629304d1cff8ddcba34e09bf1

See more details on using hashes here.

Provenance

File details

Details for the file JayDeBeApi-1.2.1-py2-none-any.whl.

File metadata

  • Download URL: JayDeBeApi-1.2.1-py2-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.2

File hashes

Hashes for JayDeBeApi-1.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 013709d069f37c258b6f86c9a0e8f356994382f9457bce9cf57367f6df0cca88
MD5 e7636559dcfb02869f68007f6c98acb5
BLAKE2b-256 ebca769e301c2d724d74ed61d3953d7909c776596b8febb2021df41149aa89e5

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page