python3.5串連MySQL

來源:互聯網
上載者:User

標籤:setup   mysqldb   .exe   com   技術分享   建立   https   source   技術   

python3.5版本特殊,以前版本的mysqldb不支援3.5了,所以這裡使用pymysql

1.解壓之後,運行cmd,進入到PyMySQL3-0.5目錄,輸入python setup.py install,進行安裝

2.安裝之後測試一下

 

1 import pymysql

 

 

 如果沒有報錯的話就表示pymysql安裝成功了,下面用資料庫測試一下

3.建立資料庫test,建立表person,隨意填入一些資料(這裡我用的sqlyog,方便一點)

4.串連資料庫

 

 1 import pymysql 2  3 # 開啟資料庫連接 4 db = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="mark", db="test", charset="utf8") 5  6 # 使用 cursor() 方法建立一個遊標對象 cursor 7 cursor = db.cursor() 8  9 # 使用 execute()  方法執行 SQL 查詢10 cursor.execute("SELECT * FROM person")11 for data in cursor:12     print(data)13 14 # 關閉資料庫連接15 db.close()

 

 

 運行結果

 

 

python3.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.