| 1 |
================================================================= |
| 2 |
JayDeBeApi - bridge from JDBC database drivers to Python DB-API |
| 3 |
================================================================= |
| 4 |
|
| 5 |
.. image:: https://img.shields.io/travis/baztian/jaydebeapi/master.svg |
| 6 |
:target: https://travis-ci.org/baztian/jaydebeapi |
| 7 |
|
| 8 |
.. image:: https://img.shields.io/coveralls/baztian/jaydebeapi/master.svg |
| 9 |
:target: https://coveralls.io/r/baztian/jaydebeapi |
| 10 |
|
| 11 |
.. image:: https://img.shields.io/badge/python-2.7,_3.5,_3.6-blue.svg |
| 12 |
:target: https://pypi.python.org/pypi/JayDeBeApi/ |
| 13 |
|
| 14 |
.. image:: https://img.shields.io/badge/jython-2.7.2-blue.svg |
| 15 |
:target: https://pypi.python.org/pypi/JayDeBeApi/ |
| 16 |
|
| 17 |
.. image:: https://img.shields.io/github/tag/baztian/jaydebeapi.svg |
| 18 |
:target: https://pypi.python.org/pypi/JayDeBeApi/ |
| 19 |
|
| 20 |
.. image:: https://img.shields.io/pypi/dm/JayDeBeApi.svg |
| 21 |
:target: https://pypi.python.org/pypi/JayDeBeApi/ |
| 22 |
|
| 23 |
The JayDeBeApi module allows you to connect from Python code to |
| 24 |
databases using Java `JDBC |
| 25 |
<http://java.sun.com/products/jdbc/overview.html>`_. It provides a |
| 26 |
Python DB-API_ v2.0 to that database. |
| 27 |
|
| 28 |
It works on ordinary Python (cPython) using the JPype_ Java |
| 29 |
integration or on `Jython <http://www.jython.org/>`_ to make use of |
| 30 |
the Java JDBC driver. |
| 31 |
|
| 32 |
In contrast to zxJDBC from the Jython project JayDeBeApi let's you |
| 33 |
access a database with Jython AND Python with only minor code |
| 34 |
modifications. JayDeBeApi's future goal is to provide a unique and |
| 35 |
fast interface to different types of JDBC-Drivers through a flexible |
| 36 |
plug-in mechanism. |
| 37 |
|
| 38 |
.. contents:: |
| 39 |
|
| 40 |
Install |
| 41 |
======= |
| 42 |
|
| 43 |
You can get and install JayDeBeApi with `pip <http://pip.pypa.io/>`_ |
| 44 |
:: |
| 45 |
|
| 46 |
$ pip install JayDeBeApi |
| 47 |
|
| 48 |
If you want to install JayDeBeApi in Jython make sure to have pip or |
| 49 |
EasyInstall available for it. |
| 50 |
|
| 51 |
Or you can get a copy of the source by cloning from the `JayDeBeApi |
| 52 |
github project <https://github.com/baztian/jaydebeapi>`_ and install |
| 53 |
with :: |
| 54 |
|
| 55 |
$ python setup.py install |
| 56 |
|
| 57 |
or if you are using Jython use :: |
| 58 |
|
| 59 |
$ jython setup.py install |
| 60 |
|
| 61 |
It has been tested with Jython 2.7.2. |
| 62 |
|
| 63 |
If you are using cPython ensure that you have installed JPype_ |
| 64 |
properly. It has been tested with JPype1 0.6.3 and 0.7.5 for Python 3 and |
| 65 |
with JPype1 0.6.3 and 0.7.0 for Python 2.7. Older JPype |
| 66 |
installations may cause problems. |
| 67 |
|
| 68 |
Usage |
| 69 |
===== |
| 70 |
|
| 71 |
Basically you just import the ``jaydebeapi`` Python module and execute |
| 72 |
the ``connect`` method. This gives you a DB-API_ conform connection to |
| 73 |
the database. |
| 74 |
|
| 75 |
The first argument to ``connect`` is the name of the Java driver |
| 76 |
class. The second argument is a string with the JDBC connection |
| 77 |
URL. Third you can optionally supply a sequence consisting of user and |
| 78 |
password or alternatively a dictionary containing arguments that are |
| 79 |
internally passed as properties to the Java |
| 80 |
``DriverManager.getConnection`` method. See the Javadoc of |
| 81 |
``DriverManager`` class for details. |
| 82 |
|
| 83 |
The next parameter to ``connect`` is optional as well and specifies |
| 84 |
the jar-Files of the driver if your classpath isn't set up |
| 85 |
sufficiently yet. The classpath set in ``CLASSPATH`` environment |
| 86 |
variable will be honored. See the documentation of your Java runtime |
| 87 |
environment. |
| 88 |
|
| 89 |
Here is an example: |
| 90 |
|
| 91 |
>>> import jaydebeapi |
| 92 |
>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver", |
| 93 |
... "jdbc:hsqldb:mem:.", |
| 94 |
... ["SA", ""], |
| 95 |
... "/path/to/hsqldb.jar",) |
| 96 |
>>> curs = conn.cursor() |
| 97 |
>>> curs.execute('create table CUSTOMER' |
| 98 |
... '("CUST_ID" INTEGER not null,' |
| 99 |
... ' "NAME" VARCHAR(50) not null,' |
| 100 |
... ' primary key ("CUST_ID"))' |
| 101 |
... ) |
| 102 |
>>> curs.execute("insert into CUSTOMER values (1, 'John')") |
| 103 |
>>> curs.execute("select * from CUSTOMER") |
| 104 |
>>> curs.fetchall() |
| 105 |
[(1, u'John')] |
| 106 |
>>> curs.close() |
| 107 |
>>> conn.close() |
| 108 |
|
| 109 |
If you're having trouble getting this work check if your ``JAVA_HOME`` |
| 110 |
environmentvariable is set correctly. For example I have to set it on |
| 111 |
my Ubuntu machine like this :: |
| 112 |
|
| 113 |
$ JAVA_HOME=/usr/lib/jvm/java-8-openjdk python |
| 114 |
|
| 115 |
An alternative way to establish connection using connection |
| 116 |
properties: |
| 117 |
|
| 118 |
>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver", |
| 119 |
... "jdbc:hsqldb:mem:.", |
| 120 |
... {'user': "SA", 'password': "", |
| 121 |
... 'other_property': "foobar"}, |
| 122 |
... "/path/to/hsqldb.jar",) |
| 123 |
|
| 124 |
Also using the ``with`` statement might be handy: |
| 125 |
|
| 126 |
>>> with jaydebeapi.connect("org.hsqldb.jdbcDriver", |
| 127 |
... "jdbc:hsqldb:mem:.", |
| 128 |
... ["SA", ""], |
| 129 |
... "/path/to/hsqldb.jar",) as conn: |
| 130 |
... with conn.cursor() as curs: |
| 131 |
... curs.execute("select count(*) from CUSTOMER") |
| 132 |
... curs.fetchall() |
| 133 |
[(1,)] |
| 134 |
|
| 135 |
Supported databases |
| 136 |
=================== |
| 137 |
|
| 138 |
In theory *every database with a suitable JDBC driver should work*. It |
| 139 |
is confirmed to work with the following databases: |
| 140 |
|
| 141 |
* SQLite |
| 142 |
* Hypersonic SQL (HSQLDB) |
| 143 |
* IBM DB2 |
| 144 |
* IBM DB2 for mainframes |
| 145 |
* Oracle |
| 146 |
* Teradata DB |
| 147 |
* Netezza |
| 148 |
* Mimer DB |
| 149 |
* Microsoft SQL Server |
| 150 |
* MySQL |
| 151 |
* PostgreSQL |
| 152 |
* many more... |
| 153 |
|
| 154 |
Contributing |
| 155 |
============ |
| 156 |
|
| 157 |
Please submit `bugs and patches |
| 158 |
<https://github.com/baztian/jaydebeapi/issues>`_. All contributors |
| 159 |
will be acknowledged. Thanks! |
| 160 |
|
| 161 |
License |
| 162 |
======= |
| 163 |
|
| 164 |
JayDeBeApi is released under the GNU Lesser General Public license |
| 165 |
(LGPL). See the file ``COPYING`` and ``COPYING.LESSER`` in the |
| 166 |
distribution for details. |
| 167 |
|
| 168 |
|
| 169 |
Changelog |
| 170 |
========= |
| 171 |
|
| 172 |
- Next version - unreleased |
| 173 |
- 1.2.3 - 2020-06-12 |
| 174 |
|
| 175 |
- Make pip install for Python 2 work by changing JPype1 requirement to older |
| 176 |
version |
| 177 |
- Make pip install for Jython work by removing JPype1 requirement for Jython |
| 178 |
- Removed cursor destructor to avoid issues with some JPype versions (please |
| 179 |
make sure you're always closing your cursors properly) |
| 180 |
|
| 181 |
- 1.2.2 - 2020-06-04 |
| 182 |
|
| 183 |
- Return (big) decimal types as long value if scale is zero (thanks |
| 184 |
to @ministat) |
| 185 |
- Fix `DECIMAL` and `NUMERIC` type conversion for Jython |
| 186 |
|
| 187 |
- 1.2.1 - 2020-05-27 |
| 188 |
|
| 189 |
- Increased thread safety. Should resolve some of the |
| 190 |
`No suitable driver found` errors (thanks to @thealmightygrant) |
| 191 |
|
| 192 |
- 1.2.0 - 2020-05-22 |
| 193 |
|
| 194 |
- Added compatibility to JPype1 0.7.2+ (thanks to @dpd) |
| 195 |
- Support `with` statement (thanks to @Szczepanov) |
| 196 |
|
| 197 |
- 1.1.2 - 2019-09-02 |
| 198 |
|
| 199 |
- Added compatibility to JPype1 0.7 (thanks to @Iverian, @Thrameos) |
| 200 |
- Dropped python 2.6 support |
| 201 |
- Fix build working with newer Maven versions |
| 202 |
- Accidently force-pushed to master branch. Sorry for that. |
| 203 |
|
| 204 |
- 1.1.1 - 2017-03-21 |
| 205 |
|
| 206 |
- Don't fail on dates before 1900 on Python < 3. |
| 207 |
|
| 208 |
- 1.1.0 - 2017-03-19 |
| 209 |
|
| 210 |
- Support BIT and TINYINT type mappings (thanks @Mokubyow for |
| 211 |
reporting the issue). |
| 212 |
|
| 213 |
- 1.0.0 - 2017-01-10 |
| 214 |
|
| 215 |
- Allow for db properties to be passed to the connect |
| 216 |
method. *Probably incompatible to code based on previous |
| 217 |
versions.* See documentation of the connect method. (Thanks |
| 218 |
@testlnord for you efforts and the patience.) |
| 219 |
|
| 220 |
- New major version due to possible api incompatibility. |
| 221 |
|
| 222 |
- 0.2.0 - 2015-04-26 |
| 223 |
|
| 224 |
- Python 3 support (requires JPype1 >= 0.6.0). |
| 225 |
|
| 226 |
- 0.1.6 - 2015-04-10 |
| 227 |
|
| 228 |
- Fix Jython handling of Java exceptions that don't subclass python Exception |
| 229 |
|
| 230 |
- Enrich exceptions with message from java SQLExceptions |
| 231 |
|
| 232 |
- Be more specific about DB API exceptions: Distinguish DatabaseError and |
| 233 |
InterfaceError. |
| 234 |
|
| 235 |
- Fix typo LONGNARCHAR vs LONGVARCHAR (thanks @datdo for reporting #4) |
| 236 |
|
| 237 |
- 0.1.5 - 2015-03-02 |
| 238 |
|
| 239 |
- Add version number to module. |
| 240 |
|
| 241 |
- Improve robustness of java to python type conversion. |
| 242 |
|
| 243 |
- Support Time type. |
| 244 |
|
| 245 |
- Add DB-API compliant exception handling. |
| 246 |
|
| 247 |
- Minor documentation improvements. |
| 248 |
|
| 249 |
- Some development related changes (Host project at github, use |
| 250 |
Travis CI, use JPype1 for tests). |
| 251 |
|
| 252 |
- 0.1.4 - 2013-10-29 |
| 253 |
|
| 254 |
- More convenient way to setup Java classpath. *Important note* |
| 255 |
check the changes to the ``connect`` method and adapt your code. |
| 256 |
|
| 257 |
- Honor ``CLASSPATH`` if used in JPype mode. |
| 258 |
|
| 259 |
- Set ``.rowcount`` properly. |
| 260 |
|
| 261 |
- Changed signature of ``.setoutputsize()`` to be DB-API compliant. |
| 262 |
|
| 263 |
- 0.1.3 - 2011-01-27 |
| 264 |
|
| 265 |
- Fixed DB-API_ violation: Use ``curs.execute('foo ?', (bar, baz))`` |
| 266 |
instead of ``curs.execute('foo ?', bar, baz)``. |
| 267 |
|
| 268 |
- Free resources after ``executemany`` call. |
| 269 |
|
| 270 |
- Improved type handling. Initial support for BLOB columns. |
| 271 |
|
| 272 |
- 0.1.2 - 2011-01-25 |
| 273 |
|
| 274 |
- ``easy_install JayDeBeApi`` should really work. |
| 275 |
|
| 276 |
- 0.1.1 - 2010-12-12 |
| 277 |
|
| 278 |
- Fixed bug #688290 "NULL values with converters error on fetch". |
| 279 |
- Fixed bug #684909 "Selecting ROWIDs errors out on fetch". |
| 280 |
|
| 281 |
- 0.1 - 2010-08-10 |
| 282 |
|
| 283 |
- Initial release. |
| 284 |
|
| 285 |
To do |
| 286 |
===== |
| 287 |
|
| 288 |
- Extract Java calls to separate Java methods to increase performance. |
| 289 |
- Check if https://code.launchpad.net/dbapi-compliance can help making |
| 290 |
JayDeBeApi more DB-API compliant. |
| 291 |
- Test it on different databases and provide a flexible db specific |
| 292 |
pluign mechanism. |
| 293 |
- SQLAlchemy modules (separate project) |
| 294 |
|
| 295 |
.. _DB-API: http://www.python.org/dev/peps/pep-0249/ |
| 296 |
.. _JPype: https://pypi.python.org/pypi/JPype1/ |