将 Python 连接到 H2

Connect Python to H2

我正在尝试建立从 python2.7 到 H2 的连接(h2-1.4.193.jar - 最新)

H2(运行 可用):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

对于 python 我正在使用 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)

结果每次我得到一个错误:Process finished with exit code -1073741819 (0xC0000005) 你对这个案子有什么想法吗?或者我可以用其他东西代替 jaydebeapi?

回答我自己的问题: 首先,我无法通过 jaydebeapi 做任何事情。 我读过 H2 支持 PostgreSQL 网络协议。我接下来的步骤是将 h2 和 python 转移到 pgsql:

H2 页面:

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 已包括在内以尝试从 Web 控制台连接。

Python: psycopg2 而不是 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"))')

因此 - 它现在可以正常工作了。已建立连接并创建 table。

Web 控制台设置:

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

Web 控制台确实连接了,但没有显示 table 列表,而是显示了很多错误:"CURRENT_SCHEMAS" is not found etc...。 PG admin 4 也无法连接。 SQuirrel 来救援 - 它已连接到此数据库,一切正常。

1.5 年后更新可能有点晚,但当前版本与 H2 连接良好,无需使用 postgres 驱动程序。

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

来源:https://pypi.org/project/JayDeBeApi/#usage