pymysql串連mysql報錯

來源:互聯網
上載者:User

標籤:pymysql 錯誤

環境:windows10+Python3.6

  1. 安裝Python

    下載路徑:https://www.python.org/downloads/windows/ 選擇自己的版本

  2. 直接安裝,然後配置環境變數

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/8D/6E/wKioL1icLX_Q6RfXAABI2xTh73A384.png-wh_500x0-wm_3-wmp_4-s_3205484833.png" title="1.png" alt="wKioL1icLX_Q6RfXAABI2xTh73A384.png-wh_50" />

3.安裝pymysql

 這裡我是直接用pip安裝的,命令:

  pip install pymysql

4.串連資料庫

  

import pymysql


# 開啟資料庫連接

db = pymysql.connect("localhost","develop","5tgb^YHN","test" )


# 使用 cursor() 方法建立一個遊標對象 cursor

cursor = db.cursor()


sql = "SELECT * from gds.pub_exchange_calendar"

# 使用 execute()  方法執行 SQL 查詢 

cursor.execute(sql)


# 使用 fetchone() 方法擷取單條資料.

# 使用fetchall() 方法取得結果集

data = cursor.fetchall()


for row in data:

id = row[0]

date = row[1]

print ("id is  : %d " % id,"date is %s" %date)



# 關閉資料庫連接

db.close()

結果報錯,如下

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/8D/6E/wKioL1icLNuxGPreAAA3q2HolL4713.png-wh_500x0-wm_3-wmp_4-s_1691038156.png" title="1.png" alt="wKioL1icLNuxGPreAAA3q2HolL4713.png-wh_50" />

後面查了下原因,是因為我的檔案名稱叫select.py  檔案名稱是關鍵字,所以改了檔案名稱就好了


5.成功運行

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/8D/70/wKiom1icLnGgQuFwAAAipQvlCjk733.png-wh_500x0-wm_3-wmp_4-s_1902748126.png" title="1.png" alt="wKiom1icLnGgQuFwAAAipQvlCjk733.png-wh_50" />

pymysql串連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.