python 基礎 9.4 遊標

來源:互聯網
上載者:User

標籤:行資料   chm   with   inux   white   proc   port   cut   重要   

一. 遊標是系統為使用者開設的一個資料緩衝區,存放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:\Python27\python.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‘]]result1Noneresult2()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)                                                                                                            

python 基礎 9.4 遊標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.