Python3中使用Mysql的用法。

來源:互聯網
上載者:User

標籤:mysqld   fetch   localhost   local   finally   success   sql   span   記錄   

一、Python2中一般使用MySqldb來調用Mysql,但是在Python3中不支援該包,使用pymysql來代替了,用法一模一樣。

二、安裝:

pip install pymysql

三、例子:

#coding utf-8import pymysqltry:    conn = pymysql.connect(host=‘localhost‘,user="root",password=‘123456‘,database=‘datarepair‘,port=3306,charset=‘utf8‘)    cursor = conn.cursor()    #cursor.execute("select rowId,Name from ruletab where ruletype=%s",(10))    cursor.executemany("select rowId,Name from ruletab where ruletype=%s",[10,20]) #一般用於批量增刪改中。    print(cursor.rowcount,cursor.rownumber)    for x in cursor.fetchall():        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rownumber))    print(‘-‘ * 50)    for x in cursor.fetchmany(2):        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rowcount))    print(‘-‘ * 50)    cursor.scroll(0,‘absolute‘) #將遊標調回0位置處。    for x in cursor.fetchmany(2):  #取2條記錄。        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rownumber))    print(cursor.rowcount,cursor.rownumber)    cursor.close()finally:    if ‘conn‘ in dir() and callable(conn) and conn.open:        conn.close()        print("closed successfully.")

 

Python3中使用Mysql的用法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.