lang
1def name2id(name): 2 print u"select id from TABLE where 名前= '%s'"%(unicode(name,'utf8'),) 3 cur.execute(u"select id from TABLE where 名前= '%s'"%(unicode(name,'utf8'),)) 4 return cur.fetchone()[0]
という、データベースを介して、名前からIDを返す簡単な関数を実装しました。
そして、
lang
1 2 for d in os.listdir(Path): 3 if d!='.DS_Store': 4 name2id(d)
と、Pathで指定されたディレクトリ直下のディレクトリ(ディレクトリ名='名前')一覧を取得して、name2id関数に渡しました。
しかし、エラーが発生しました。
'''
in name2id
return cur.fetchone()[0]
TypeError: 'NoneType' object has no attribute 'getitem'
'''
つまり、検索に失敗しているという事のようです。
不可解なのが、
'''
select id from TABLE where 名前= '佐藤'
001
select id from TABLE where 名前= '田中'
002
select id from TABLE where 名前= '山田'
Traceback (most recent call last):
'''
という具合に、佐藤、田中は検索に成功しているにも関わらず、山田だけ検索に失敗します。
また、更に不可解なのが、
lang
1print name2id('山田')
と、os.listdir()を関わらせず、関数に直接クエリを投げた所、問題なく結果が表示されました。
文字コードなど色々と検討してみましたが、有用な解決策はついぞ発見できませんでした。
そこで、皆様に原因と解決策をご指導頂きたいと思います。
どうかよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2014/11/23 15:20
2014/11/23 16:31
2014/11/24 04:57