Windows7でPython3.5環境で cx_Oracleを使う

2016年3月18日 Posted by PURGE

pythonでOracleへ接続したい。

import cx_Oracle
import os
os.environ["NLS_LANG"] = "Japanese_Japan.AL32UTF8"

tns = cx_Oracle.makedsn("192.168.0.1",1521,"ORASID")
connection = cx_Oracle.connect("user","pass",tns)
#connection = cx_Oracle.connect("user","pass","(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1) (PORT=1521))(CONNECT_DATA=(SID=ORASID)))")

cursor = connection.cursor()
agent_cd = '7'
sql = """
    select agent_cd, agent_name
      from agents
     where agent_cd = :agent_cd
    """
cursor.execute(sql, agent_cd)
rows = cursor.fetchall()
for agent in rows:
    print(agent[0], agent[1])


コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です