python oracle 操作

  1. 下载cx_oracle(根据自己oracle的版本,11g或者是12c),安装
    参考资料:
    http://www.oracle.com/technetwork/articles/dsl/prez-python-queries-101587.html
    http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/python_db/python_db.htm
    https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

代码:

import cx_Oracle
con = cx_Oracle.connect(username, password, host:port/sid)
cursor = con .cursor()

# 查询
cursor.execute("SELECT * FROM employees ORDER BY emp_no")
rows = cursor.fetchall()
for row in rows:
    print row

data = [
  ('Jane', date(2005, 2, 12)),
  ('Joe', date(2006, 5, 23)),
  ('John', date(2010, 10, 3)),
]
# 执行many时,一定是一个list
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)

insert_stmt = (
  "INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
  "VALUES (%s, %s, %s, %s)"
)
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
# 执行execute时,一定是一个touple
cursor.execute(insert_stmt, data)

# 关闭连接
con.close()

你可能感兴趣的:(oracle,python)