[oracle@node01 python]$ cat a1.py
import cx_Oracle
import cx_Oracle
conn = cx_Oracle.connect('vxspace/newja01@120.26.224.164/oadb')
cursor = conn.cursor()
cursor.execute("SELECT * FROM TEST")
rows = cursor.fetchall()
for row in rows:
print row
cursor.close()
conn.close()
class Student(object):
def __init__(self, name,score):
self.name = name
self.score = score
def print_score(self):
print '%s: %s' % (self.name, self.score -2)
print __name__
if __name__ == "__main__":
Student('aaa',88).print_score()
--------------------------------------------------------
直接当作py文件使用
[oracle@node01 Web]$ cat HR.py
import cx_Oracle
class Hr(object):
def __init__(self, user,passwd,ip,service):
self.user=user
self.passwd=passwd
self.ip=ip
self.service=service
r=user+'/'+passwd+'@'+ip+'/'+service
print r
self.conn = cx_Oracle.connect(r)
self.cursor = self.conn.cursor()
def query(self):
self.cursor.execute("SELECT * FROM employees")
rows = self.cursor.fetchall()
for row in rows:
print row
self.cursor.close()
self.conn.close()
print __name__
if __name__ == "__main__":
Hr('hr','hr','192.168.137.2','serv').query()
[oracle@node01 Web]$ python HR.py
__main__
hr/hr@192.168.137.2/serv
(100, 'Steven', 'King', 'SKING', '515.123.4567', datetime.datetime(1987, 6, 17, 0, 0), 'AD_PRES', 24000.0, None, None, 90)
当作模块使用:
[oracle@node01 python]$ python a7.py
Traceback (most recent call last):
File "a7.py", line 1, in <module>
from mycompany.web.Hr import *
ImportError: No module named mycompany.web.Hr
[oracle@node01 python]$ cat a7.py
from mycompany.web.Hr import *
h= Hr('hr','hr','192.168.137.2','serv')
h.query()
[oracle@node01 python]$ python a7.py
mycompany.web.Hr
hr/hr@192.168.137.2/serv
(100, 'Steven', 'King', 'SKING', '515.123.4567', datetime.datetime(1987, 6, 17, 0, 0), 'AD_PRES', 24000.0, None, None, 90)
[oracle@node01 python]$ python mycompany/web/Hr.py
__main__
hr/hr@192.168.137.2/serv
(100, 90)
(105, 60)
def query(self, employee_id1, employee_id2):
select_sql = """select employee_id, department_id from employees
where employee_id in (:1, :2)"""
self.cursor.execute(select_sql, (employee_id1, employee_id2))
rows = self.cursor.fetchall()
for row in rows:
print row
self.cursor.close()
self.conn.close()