ibm_db と SQLAlchemyの組み合わせでエラーが発生する



Pylons 0.9.7にアップデートしてからだと思うのですが、
DB2 V9.5 + ibm_db_sa + SQLAlchemy 0.4.6の組み合わせで
動作させると


DatabaseError: (DatabaseError) ibm_db_dbi::DatabaseError: Binding Error:
'INSERT INTO sample (id, view_flag, document) VALUES (?, ?, ?)' ['1', 0, u'\u30b3\u30e1\u30f3\u30c8']


というエラーが発生するようになりました。
おそらく、makoのデフォルトエンコーディングがutf8に
変更された影響だと思います。


Unicode handling in insertion statements


2008/11/17
Unicode is not yet supported on ibm_db_sa. I have it in my
future plans and you will see unicode support in near future.


ibm_db_saでは今のところunicodeをサポートしてないよ。
近い将来サポートする予定だけど。


要するに、SQLAlchemyはSQL文を生成する際、
渡された文字列をunicodeに自動変換するのですが、
この変換が行なわれるため上手くいかないようです。

うーん。日本語に限らずエラーになるのですが、
みんなどうやって使ってるんだろ?



[Imdbpy-devel] Bug: imdbpy2sql.py and DB2 9.5

ここのスレッドの中にヒントがありました。


Anyway, now what's needed to make IMDbPY work is a way to insert
unicode strings using SQLAlchemy.
What if (just to do a fast test) you change this line of the
setConnection function:
params = {'encoding': encoding}

to this one:
params = {'encoding': encoding, 'convert_unicode': True}

does it works?


convert_unicodeというパラメータをTrueにして試してみてとのこと。

development.iniにパラメータを追加します。


# SQLAlchemy database URL
sqlalchemy.url = ibm_db_sa://db2inst1:password@localhost:50000/test
###以下の行を追加
sqlalchemy.convert_unicode = true



これで動かしてみると、ちゃんと登録できました。

ibm_db_saが更新されるまではこれで逃げようと思います。


もどる