Filemaker to Python (2) - Python側の準備

前の記事の続きです。

pyODBCのインストール

これは、ホントにそのままesay_installであっさり行きました。

$ easy_install pyodbc
Searching for pyodbc
Reading http://pypi.python.org/simple/pyodbc/
Reading http://code.google.com/p/pyodbc
Reading http://code.google.com/p/pyodbc/downloads/list
Best match: pyodbc 3.0.7
Downloading http://pyodbc.googlecode.com/files/pyodbc-3.0.7.zip
Processing pyodbc-3.0.7.zip
Running pyodbc-3.0.7/setup.py -q bdist_egg --dist-dir /var/folders/z_/45w_7yf1701gxyghp070g5t40000gn/T/easy_install-IeVfC3/pyodbc-3.0.7/egg-dist-tmp-6vIccU
warning: no files found matching 'tests/*'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
clang: warning: argument unused during compilation: '-mno-fused-madd'
zip_safe flag not set; analyzing archive contents...
Adding pyodbc 3.0.7 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/pyodbc-3.0.7-py2.7-macosx-10.8-intel.egg
Processing dependencies for pyodbc
Finished processing dependencies for pyodbc

PythonからFilemaker DBにアクセスしてみた

ここまでくれば、あとはPythonからpyODBCを呼んであげて、

$ python
Python 2.7.4 (v2.7.4:026ee0057e2d, Apr  6 2013, 11:43:10) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc

ここからの

>>> cxcn = pyodbc.connect('DSN=CWord;UID=xxxxxx;PWD=xxxxxxxxxx')
>>> cursor = cxcn.cursor()
>>> cursor.execute("select 単語, 拼音 from CWordsDB")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pyodbc.ProgrammingError: ('42000', '[42000] [FileMaker][FileMaker] FQL0001/(1:8): There is an error in the syntax of the query. (8310) (SQLExecDirectW)')

今、冷静に見ると2つのカラムの間のカンマ(,)が全角になっているのが問題だと分かりますが、とっさにFilemaker側で項目名をアルファベットの名称に変更しました。

で、仕切り直し。

>>> cursor.execute("select Word, Pinyin from CWordsDB")
<pyodbc.Cursor object at 0x100763510>
>>> row = cursor.fetchone()

お、行けた。じゃ、中身を出力してみる。

>>> print row
('???', 'ban1 yun4 gong1')

おっっと。文字化け。1個思い当たるフシが。ODBC AdministratorのAdvanced languageの設定で、Unicodeをオンにしてみる。やっぱり自動言語設定には無理があったかな?

f:id:deutschina:20130720140622p:plain

では、仕切り直し

DSNの設定を変えたので、念のため読み込み直してみる。

>>> cxcn = pyodbc.connect('DSN=CWord;UID=xxxxxx;PWD=xxxxxxxxxx')
>>> cursor = cxcn.cursor()
>>> cursor.execute("select Word, Pinyin from CWordsDB")
<pyodbc.Cursor object at 0x100763570>
>>> row = cursor.fetchone()

良い感じ。では出してみよう。

>>> print row
('\xe6\x90\xac', 'ban1 yun4 gong1')

見覚えのある文字列が出てきたような。でも短くないか?ということで

>>> print row[0]
搬
>>> print row[1]
ban1 yun4 gong1

実際の文字列は「搬运工」なんだけれども、なぜか最初の文字しか拾われていない。

うむ。もう少し調べてみないと分からないな。

(多分つづく)