Connect Python to H2

21.7k views Asked by At

I'm trying to make a connection from python2.7 to H2 (h2-1.4.193.jar - latest)

H2 (is running and available): java -Dh2.bindAddress=127.0.0.1 -cp "E:\Dir\h2-1.4.193.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcpPort 15081 -baseDir E:\Dir\db

For python I'm using jaydebeapi:

import jaydebeapi

conn = jaydebeapi.connect('org.h2.Driver', ['jdbc:h2:tcp://localhost:15081/db/test', 'sa', ''], 'E:\Path\to\h2-1.4.193.jar')
curs = conn.cursor()
curs.execute('create table PERSON ("PERSON_ID" INTEGER not null, "NAME" VARCHAR not null, primary key ("PERSON_ID"))')
curs.execute("insert into PERSON values (1, 'John')")
curs.execute("select * from PERSON")
data = curs.fetchall()
print(data)

As a result everytime I get an error: Process finished with exit code -1073741819 (0xC0000005) Do you have any ideas about this case? Or maybe there is something else that I can use instead of the jaydebeapi?

2

There are 2 answers

0
Angelo Hulshout On BEST ANSWER

Perhaps a bit late for an update after 1.5 years, but the current version connects fine with H2, without having to use a postgres driver.

conn = jaydebeapi.connect("org.h2.Driver", "jdbc:h2:~/test", ["sa", ""], "/Users/angelo/websites/GEPR/h2/bin/h2-1.4.197.jar",)

source: https://pypi.org/project/JayDeBeApi/#usage

0
IgorZ On

Answering my own question: First of all I could not do anything through the jaydebeapi. I've read that H2 supports PostgreSQL network protocol. My next steps were to transfer h2 and python into pgsql:

H2 pg:

java -Dh2.bindAddress=127.0.0.1 -cp h2.jar;postgresql-9.4.1212.jre6.jar org.h2.tools.Server -baseDir E:\Dir\h2\db

TCP server running at tcp://localhost:9092 (only local connections)
PG server running at pg://localhost:5435 (only local connections)
Web Console server running at http://localhost:8082 (only local connections)

postgresql.jar was included to try to connect from Web Console.

Python: psycopg2 instead of jaydebeapi:

import psycopg2

conn = psycopg2.connect("dbname=h2pg user=sa password='sa' host=localhost port=5435")
cur = conn.cursor()
cur.execute('create table PERSON ("PERSON_ID" INTEGER not null, "NAME" VARCHAR not null, primary key ("PERSON_ID"))')

As a result - it's working now. Connection was established and table was created.

Web Console settings:

Generic PostgreSQL
org.postgresql.Driver
jdbc:postgresql://localhost:5435/h2pg
name: sa, pass: sa

Web Console did connect but did not show me table list and showed many errors instead: "CURRENT_SCHEMAS" is not found etc.... PG admin 4 was not also able to connect. SQuirrel to the rescue - it had connected to this db and all is working fine there.