Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect Python to H2

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?

like image 965
IgorZ Avatar asked Jan 05 '17 19:01

IgorZ


People also ask

How do I connect to H2 database?

Click Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.

What is H2 in Python?

h2 is a HTTP/2 protocol stack, written entirely in Python. The goal of h2 is to be a common HTTP/2 stack for the Python ecosystem, usable in all programs regardless of concurrency model or environment.

How do I pair my H2 remote database?

In server mode, you need to use one of the following JDBC URLs: jdbc:h2:tcp://localhost/~/test connect to the 'test' database in the user home directory on the server (local computer). jdbc:h2:tcp://192.168.1.3:9092 //data/test connect to the 'test' database in the /data directory on the remote server.

How do you connect to H2 spring?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.


1 Answers

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.

like image 185
IgorZ Avatar answered Sep 30 '22 19:09

IgorZ