zoukankan      html  css  js  c++  java
  • 第 1 部分:查询最佳应践:

    第 1 部分:查询最佳应践:
    
    [oracle@node01 python]$ python a1.py
    Traceback (most recent call last):
      File "a1.py", line 2, in <module>
        db = cx_Oracle.connect('hr', 'hrpwd', 'localhost:1521/XE')
    cx_Oracle.DatabaseError: ORA-12541: TNS:no listener
    
    
    [oracle@node01 python]$ cat a1.py 
    import cx_Oracle
    db = cx_Oracle.connect('query', 'kjk7787czcb', '120.26.224.164:1521/oadb')
    [oracle@node01 python]$ python a1.py
    
    
    [oracle@node01 python]$ cat a1.py 
    import cx_Oracle
    db = cx_Oracle.connect('query', 'kjk7787czcb', '120.26.224.164:1521/oadb')
    print db.version;
    [oracle@node01 python]$ python a1.py
    11.2.0.4.0
    
    [oracle@node01 python]$ cat a1.py 
    import cx_Oracle
    db = cx_Oracle.connect('query', 'kjk7787czcb', '120.26.224.164:1521/oadb')
    print db.version;
    print db.dsn
    [oracle@node01 python]$ python a1.py
    11.2.0.4.0
    120.26.224.164:1521/oadb
    
    
    游标对象:
    
    
    你可以使用连接对象的cursor()方法定义任意数量的游标,简单的程序使用一个游标就可以了,
    
    该游标可以一再地重复使用,但较大的项目可能需求几个不同的游标:
    
    应用程序逻辑通常需要明确区分针对数据库发出的语句的各个处理阶段。
    
    这有助于更好地理解性能瓶颈并编写更快且经过优化的代码。语句分3个阶段:
    
    1.分析(可选)
    
    cursor.parse('delete from vxspace.test')
    
    实际上并不需要调用,因为在执行阶段会自动分析SQL语句
    
    [oracle@node01 python]$ python a1.py
    11.2.0.4.0
    120.26.224.164:1521/oadb
    Traceback (most recent call last):
      File "a1.py", line 6, in <module>
        print cursor.parse('select aa from dual')
    cx_Oracle.DatabaseError: ORA-00904: "AA": invalid identifier
    
    
    [oracle@node01 python]$ python a1.py
    11.2.0.4.0
    120.26.224.164:1521/oadb
    Traceback (most recent call last):
      File "a1.py", line 6, in <module>
        print cursor.parse('select * from dba_tables')
    cx_Oracle.DatabaseError: ORA-00942: table or view does not exist
    
    
    
    [oracle@node01 python]$ cat a1.py 
    import cx_Oracle
    db = cx_Oracle.connect('query', 'kjk7787czcb', '120.26.224.164:1521/oadb')
    print db.version;
    print db.dsn
    cursor = db.cursor()
    print cursor.parse('delete from vxspace.test')
    [oracle@node01 python]$ python a1.py
    11.2.0.4.0
    120.26.224.164:1521/oadb
    Traceback (most recent call last):
      File "a1.py", line 6, in <module>
        print cursor.parse('delete from vxspace.test')
    cx_Oracle.DatabaseError: ORA-01031: insufficient privileges
    
    
    2.执行:
    
    
    
    获取(可选)— 仅用于查询(因为 DDL 和 DCL 语句不返回结果)。在不执行查询的游标上,这些方法将引发 InterfaceError 异常。
    cx_Oracle.Cursor.fetchall() 
    以字节组列表形式获取结果集中的所有剩余行。如果没有剩余的行,它返回一个空白列表。获取操作可以通过设置游标的 arraysize 属性进行调整,
    
    该属性可设置在每个底层请求中从数据库中返回的行数。arraysize 的设置越高,需要在网络中往返传输的次数越少。arraysize 的默认值为 1。
    
    
    [oracle@node01 python]$ cat a2.py 
    import cx_Oracle
    from pprint import pprint
    
    conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
    cursor = conn.cursor()
    
    cursor.execute("SELECT * FROM TEST")
    pprint(cursor.fetchall())
    
    
    数据类型:
    
    ('select * from test ', <cx_Oracle.LOB object at 0x15c3508>, '7wnjcsyk0021v', 7564, 15536, 13160, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, '2017-07-28/05:19:49', 0, 1, 7, 0, 178, 0, 0, 0, 1848, 0, 0, 30, 3, 'ALL_ROWS', None, 'xe2x89xfbx89xe4&xa8x00Nx01x10x00xaexf9xc3xe2xcfxea3x10VAEUQx95!x10UUUx15ETUXYx15UDx96ex85x1dUx11x05x85UUQUQQ"UTx15xa0xeax0cUQEBeETTDx90x81Vnx00x16x96xc6xa3UEx15x01x02Tx15PDx16x15Wx91x10xa8Enx82
    P@ x00x00 x00x00x00x00x01x00x00x10x00x00x00x02x00 x80x00}x00x00x00xc0.x00x00x002x00x10x10x00x00x80x83xffx9bY	x00x00`x17x00@nx82
    PFF& @&# x03x00 x000 xa0xa0PPxa0@x01 x00x00@x1fx00x00x00xa5xa0xa0xa0@x86>x00x00@x06xfc0 x00 x00x00xf0xffx0fx00x80@ x03x04x00x02x00@6x00', 1446999711, 5, 5, 'SYSTEM', 0, 'x00x00x00x00alxf6p', 2751465531, 1369376869, 1357081020, 'PL/SQL Developer', 1190136663, 'SQL xe7xaax97xe5x8fxa3 - xe6x96xb0xe5xbbxba', 2127054360, 0, None, 5000, 6941, None, 'x00x00x00x00aqx86@', 'N', 'VALID', 0, datetime.datetime(2017, 7, 28, 5, 19, 49), 'N', 'N', 'N', 0, None, None, None, 0, 0, 7199376810451276368, 7199376810451276368, datetime.datetime(2017, 7, 28, 5, 19, 48), None, 0, 0, 57344, 7, 57344, 0, 0, 0, 1, 2, 0, 0)
    ('select col#,intcol#,reftyp,stabid,expctoid from refcon$ where obj#=:1 order by intcol# asc', <cx_Oracle.LOB object at 0x15c3580>, '9g485acn2n30m', 19771, 6040, 4592, 37, 1, 1, 1, 1, 111, 111, 0, 111, 0, 6, '2017-07-28/05:13:55', 0, 22, 3, 0, 715, 0, 0, 0, 900, 0, 0, 0, 3, 'CHOOSE', 1, 'xe2x89xfbx89xe4&xa8x00Nx01x10x00xaexf9xc3xe2xcfxea3 VAEUQx95!x10UUUx15ETUXYx15UDx96ex85x1dUx11x05x85UUQUQQ"UTx15xa0xeax0cUQEBeETTDx90x81Vnx08x16x96xc6xa3UEx15x01x02Tx15PDx16x15Wx91x10xa8Enx82
    P@ x00x00 x00x00x00x00x01x00x00x10x00x00x00x02x00 x80x00}x00x00x00xc0.x00x00x002x00x10x10x00x00x80x83xffx9bY	x00x00`x17x00@nx82
    @PFF& @&# x03x00 x000 xa0xa0PPxa0@x01 x00x00@x1fx00x00x00xa5xa0xa0xa0@x86>x00x00@6x02xfc0 x00 x00x00xf0xffx0fx00x80@ x03x04x00x02x00@6x00', 3992828504, 0, 0, 'SYS', 0, 'x00x00x00x00auxb5xc0', 673844243, 3302995849, 3483575214, None, 0, None, 0, 0, None, 12999, 15679, None, 'x00x00x00x00auxb1@', 'N', 'VALID', 0, datetime.datetime(2017, 7, 28, 5, 44, 38), 'N', 'N', 'N', 0, None, None, None, 0, 0, 1008029041321349622, 1008029041321349622, datetime.datetime(2017, 7, 28, 5, 45, 55), 'xbexdax0b x04x00YzWx93x00x01x01xc0x02x16x03xc2x07x13', 0, 0, 24576, 3, 24576, 0, 0, 0, 23, 147, 0, 0)
    
    遇到CLOB 的时候
    
    
    绑定变量模式:
    
    正如Oracle大师,绑定变量是数据库开发的核心原则,它们不仅
    
    执行:
    select * from t100 a where a.sample_id=1776;
    select * from t100 a where a.sample_id=1629;
    select * from t100 a where a.sample_id=1392;
    select * from t100 a where a.sample_id=992;
    select * from t100 a where a.sample_id=129;
    
    
    SQL> select * from (select sql_text from v$sql a where a.PARSING_SCHEMA_NAME='SYSTEM' order by a.LAST_ACTIVE_TIME desc)
    where rownum<10  2  ;
    
    SQL_TEXT
    --------------------------------------------------------------------------------
    select * from t100 a where a.sample_id=1629
    select * from t100 a where a.sample_id=1392
    select * from t100 a where a.sample_id=992
    select * from t100 a where a.sample_id=1776
    
    
    
    
    SELECT * FROM T200 where sid>:1 and serial#>:
    
    [oracle@node01 python]$ cat a2.py 
    import cx_Oracle
    from pprint import pprint
    import time
    
    conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM T200 where sid>:1 and serial#>:2', ('500', '50'))
    print cursor.bindnames()
    rows = cursor.fetchall()
    for row in rows:
        print row
        time.sleep(10)
        
    cursor.close()
    conn.close() 

  • 相关阅读:
    python笔记之re模块学习
    python笔记之面向对象
    C# 静态类和非静态类(实例类)
    占位符的使用
    数据类型和数据类型转换
    win7 安装 memcached
    php 汉字转换成拼音
    apache window环境下本地配置虚拟主机
    在浏览器输入一个网址到得到页面的过程
    浅谈线程池ThreadPoolExecutor核心参数
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349594.html
Copyright © 2011-2022 走看看