Last active
February 29, 2016 14:23
-
-
Save yatt/a23f77cc06c39bad40c5 to your computer and use it in GitHub Desktop.
peeweeからOracleに接続しようとして挫折
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python2.7 | |
# coding: utf-8 | |
import peewee | |
import cx_Oracle | |
import os | |
class OracleDatabase(peewee.Database): | |
def _connect(self, database, **kwargs): | |
os.environ['NLS_LANG'] = 'JAPANESE_Japan.AL32UTF8' | |
username = kwargs['username'] | |
password = kwargs['password'] | |
conn = cx_Oracle.connect(username, password, database) | |
return conn | |
database = OracleDatabase( | |
'XXX.XXX.XXX.XXX/SID', | |
username='username', | |
password='password', | |
) | |
class MyModel(peewee.Model): | |
model_id = peewee.IntegerField(null=False) | |
class Meta: | |
database = database | |
db_table = 'my_model' | |
def main(): | |
MyModel.create_table() | |
m = MyModel.create(model_id=100) | |
if __name__ == '__main__': | |
main() | |
""" | |
Traceback (most recent call last): | |
File "oracledriver.py", line 45, in <module> | |
main() | |
File "oracledriver.py", line 39, in main | |
m = MyModel.create(model_id=100) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 4494, in create | |
inst.save(force_insert=True) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 4680, in save | |
pk_from_cursor = self.insert(**field_dict).execute() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3213, in execute | |
cursor = self._execute() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2628, in _execute | |
return self.database.execute_sql(sql, params, self.require_commit) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3461, in execute_sql | |
self.commit() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3285, in __exit__ | |
reraise(new_type, new_type(*exc_args), traceback) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3454, in execute_sql | |
cursor.execute(sql, params or ()) | |
peewee.DatabaseError: ORA-01036: 変数の名前/数が無効です。 | |
Error in sys.excepthook: | |
Traceback (most recent call last): | |
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 63, in apport_excepthook | |
from apport.fileutils import likely_packaged, get_recent_crashes | |
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 5, in <module> | |
from apport.report import Report | |
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 16, in <module> | |
from xml.parsers.expat import ExpatError | |
File "/usr/lib/python2.7/xml/parsers/expat.py", line 4, in <module> | |
from pyexpat import * | |
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: XML_SetHashSalt | |
Original exception was: | |
Traceback (most recent call last): | |
File "oracledriver.py", line 45, in <module> | |
main() | |
File "oracledriver.py", line 39, in main | |
m = MyModel.create(model_id=100) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 4494, in create | |
inst.save(force_insert=True) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 4680, in save | |
pk_from_cursor = self.insert(**field_dict).execute() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3213, in execute | |
cursor = self._execute() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2628, in _execute | |
return self.database.execute_sql(sql, params, self.require_commit) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3461, in execute_sql | |
self.commit() | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3285, in __exit__ | |
reraise(new_type, new_type(*exc_args), traceback) | |
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3454, in execute_sql | |
cursor.execute(sql, params or ()) | |
peewee.DatabaseError: ORA-01036: 変数の名前/数が無効です。 | |
""" | |
""" | |
生成されるSQL: INSERT INTO "my_model" (model_id) VALUES (?) | |
パラメータ: [100] | |
cx_Oracleではプレースホルダを :1 や :field_name で書く。 ? は使え無いのでエラーになる | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment