python 3.5 串連mysql資料庫

來源:互聯網
上載者:User

標籤:調用   切換目錄   自己   code   close   where   mit   effect   pymysql   

python 3.5 要串連mysql資料庫,必須先安裝pymysql模組,該模組可以操作mysql資料。

1、安裝pymysql模組:使用pip進行安裝

cmd開啟運行模式,切換目錄到pip的script目錄下,後,輸入pip install pymysql,enter鍵後,就會自己運行安裝

2、寫代碼串連mysql資料庫:

說明:

import pymysql:匯入剛才安裝的pymysql模組,這時我們就可以調用該模組的函數來進行操作

con=pymysql.connect():調用pymysql下的connect()函數串連資料,connect()函數中輸入參數:host=‘‘,port=3306,user=‘‘,password=‘‘,db=‘‘(根據自己項目的資料庫資訊正確填寫)

cur=con.cursor():只是串連了資料庫,是不能進行操作的,需要建立遊標,通過con下的cursor()函數進行建立

cur.execute("select * from lu_order where customer_code=‘test‘ "):通過cur下的execute()函數去執行sql語句,sql語句可以是查詢、建立、插入、更新等;這個時候獲得的是符合該sql語句的資料總條數

⑤cur.fetchone():fetchone()函數可獲得第一條資料

⑥cur.fetchmany(effect_row):獲得effect_row的所有資料,用for迴圈去列印出所有的資料

⑦cur.close():關閉遊標

⑧con.commit():對資料庫進行了某些sql操作之後,需要提交事務,這個函數就是這個意思

⑨con.close():關閉資料庫連接

⑩??

 

python 3.5 串連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.