• python 基础 9.4 游标


    一. 游标是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。
          用户可以用SQL 语句逐一从游标中获取记录,并赋值给主变量,交由python
    进一步处理,一组主变量一次只能存放一条记录。
           仅使用主变量并不能完全满足SQL 语句向应用程序输出数据的要求
     
    1.游标和游标的优点
       在数据库中,游标是一个十分重要的概念。游标提供了一种从表中检索出的数据进行操作的灵活手段,就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标总是与一条SQL 选择语句相关联因为游标由结果集(可以是零条,一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。当决定对结果进行处理时,必须声明一个指向该结果的游标。
           常用 方法:
                      cursor(): 创建游标对象
                       close(): 关闭游标对象
                       fetchone(): 得到结果集的下一行
                       fetchmany([size = cursor.arraysize]):得到结果集的下几行
                       fetchall():得到结果集中剩下的所有行
                      excute(sql[,args]): 执行一个数据库查询或命令
                      executemany(sql,args):执行多个数据库查询或命令
     
    #/usr/bin/python
    #coding=utf-8
    #@Time   :2017/11/22 13:39
    #@Auther :liuzhenchuan
    #@File   :游标.py
     
    ##从另一个python脚本中调用
     
     
     
    from mysqloperate import connect_mysql
     
    if __name__ == '__main__':
        sql = 'select *from test'
        sql1 = "insert into test(id) value (%s);"
        param = []
        for i in xrange(100,130):
            param.append([str(i)])
        print param
        cnx = connect_mysql()
        cus = cnx.cursor()
        # print dir(cus)
        try:
            cus.execute(sql)
            #取多行数据库结果
            cus.executemany(sql1, param)
            result1 = cus.fetchone()
            print 'result1'
            print result1
     
            result2 = cus.fetchmany(3)
            print 'result2'
            print result2
     
            result3 = cus.fetchall()
            print 'result3'
            print result3
            cus.close()
            cnx.commit()
     
        except Exception  as e:
            cnx.rollback()
            raise e
        except TypeError as c:
            raise c
        finally:
            cnx.close()
     
    >>>
    C:Python27python.exe "E:/猿课python脚本/mysql 数据库/游标.py"
    [['100'], ['101'], ['102'], ['103'], ['104'], ['105'], ['106'], ['107'], ['108'], ['109'], ['110'], ['111'], ['112'], ['113'], ['114'], ['115'], ['116'], ['117'], ['118'], ['119'], ['120'], ['121'], ['122'], ['123'], ['124'], ['125'], ['126'], ['127'], ['128'], ['129']]
    result1
    None
    result2
    ()
    result3
    ()
     
    Process finished with exit code 0
     
     
    在linux上数据库上查询
    mysql> select *from test;
    +-----+
    | id  |
    +-----+
    | 100 |
    |  99 |
    |  95 |
    |  95 |
    |  98 |
    |  97 |
    |  96 |
    |  95 |
    | 100 |
    | 101 |
    | 102 |
    | 103 |
    | 104 |
    | 105 |
    | 106 |
    | 107 |
    | 108 |
    | 109 |
    | 110 |
    | 111 |
    | 112 |
    | 113 |
    | 114 |
    | 115 |
    | 116 |
    | 117 |
    | 118 |
    | 119 |
    | 120 |
    | 121 |
    | 122 |
    | 123 |
    | 124 |
    | 125 |
    | 126 |
    | 127 |
    | 128 |
    | 129 |
    +-----+
    38 rows in set (0.04 sec)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    Web前段学习索引
    Python学习索引
    Centos7下安装Docker(详细的新手教程)
    Django部署阿里云服务器(nginx+uwsgi)
    (转)Mongo db 与mysql 语法比较
    (转)mongo基本常用命令
    python操作mongodb的基本操作命令
    http响应码
    python回调函数
    Flask框架的使用
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7880442.html
Copyright © 2020-2023  润新知