zoukankan      html  css  js  c++  java
  • python 面向对象开发例子

    [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() 
    


    
                                        
    
  • 相关阅读:
    【vim】分割窗口、标签页与Quickfix窗口
    新手学cocos2dx,centos7下的安装过程
    外部排序,杀鸡焉用牛刀?
    5亿整数的大文件,怎么排?
    【Hadoop】HDFS
    你好,树
    写给博客园博客团队,md的预览在哪里?
    老菜鸟致青春,程序员应该选择java 还是 c#-
    高性能server分析
    高性能服务端漫谈
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349591.html
Copyright © 2011-2022 走看看