2011-12-07 58 views
0

SQLAlchemy是否支持H2 db?我正在使用金字塔並希望連接到H2數據庫。如果使用的是Postgres方言,我收到提示如下所示:SQLAlchemy是否支持H2DB?

File "/Users/homecamera/gotocamera/hcadmin/env/lib/python2.7/site-packages/SQLAlchemy-0.7.3-py2.7-macosx-10.4-x86_64.egg/sqlalchemy/dialects/postgresql/base.py", line 871, in initialize 
super(PGDialect, self).initialize(connection) 
File "/Users/homecamera/gotocamera/hcadmin/env/lib/python2.7/site-packages/SQLAlchemy-0.7.3-py2.7-macosx-10.4-x86_64.egg/sqlalchemy/engine/default.py", line 181, in initialize 
self.get_isolation_level(connection.connection) 
File "/Users/homecamera/gotocamera/hcadmin/env/lib/python2.7/site-packages/SQLAlchemy-0.7.3-py2.7-macosx-10.4-x86_64.egg/sqlalchemy/dialects/postgresql/base.py", line 910, in get_isolation_level 
cursor.execute('show transaction isolation level') 
ProgrammingError: Syntax error in SQL statement "SELECT "; expected "TOP, LIMIT, DISTINCT, ALL, *, NOT, EXISTS"; SQL statement: 
show transaction isolation level [42001-140] 
DETAIL: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT "; expected "TOP, LIMIT, DISTINCT, ALL, *, NOT, EXISTS"; SQL statement: 
show transaction isolation level [42001-140] 

回答

2

AFAIK有一個爲要麼HSQLDB方言或本地方言H2沒有官方的支持。 使用H2的Postgres方言(不使用HSQLDB)肯定會導致錯誤。

嘗試sqlalchemy-jython並使用H2方言可能會更好。

+0

感謝您的信息。我目前使用psycopg直接連接到H2。 – carbotex

+0

@carbotex,請問,請說清楚你是怎麼做的?我努力讓psycopg連接到內存數據庫。我無法讓它通過驗證步驟。任何線索表示讚賞。 – tavlima

0

爲了防止任何人再次絆倒這一點,我試圖讓這個運行(作爲替代sqlite),它只是部分工作,唯一的驅動程序工作是pg8000。

隨着使用的服務器上運行:

nohup java -cp /opt/h2/bin/h2*.jar org.h2.tools.Server -pg -pgAllowOthers -pgPort 5435 -baseDir /opt/h2-data & 

這個代碼在SQLAlchemy的:

from sqlalchemy import create_engine 
engine = create_engine('postgresql+pg8000://sa:[email protected]:5435/main') 
engine.execute("SELECT 1") 

但是這個代碼拋出一個異常: 從sqlalchemy_utils導入create_database create_database('PostgreSQL的+ pg8000: // sa:sa @ localhost:5435/main')

例外:

sqlalchemy.exc.ProgrammingError: (pg8000.core.ProgrammingError) ('ERROR', 
'HY000', 'General error: "java.lang.IllegalStateException: output binary 
format is undefined" [50000-196]', 'org.h2.jdbc.JdbcSQLException: General 
error: "java.lang.IllegalStateException: output binary format is undefined" 
[50000-196]') [SQL: 'select version()']