python 操作資料庫(mysql)

來源:互聯網
上載者:User

標籤:python 操作資料庫

1、安裝外掛程式網上很多不在囉嗦。

2、串連資料庫後建立一個表

MySQLdbconn = MySQLdb.connect(        =,        =,        =,        =,        =,        )cur = conn.cursor()cur.execute()

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/76/51/wKioL1ZO-0HjHFVZAAAaKB1VhV8560.png" title="執行結果" width="132" height="100" border="0" hspace="0" vspace="0" style="width:132px;height:100px;" alt="wKioL1ZO-0HjHFVZAAAaKB1VhV8560.png" />

3、增加一條資料

     

MySQLdbconn = MySQLdb.connect(        =,        =,        =,        =,        =,        )cur = conn.cursor()cur.execute()  #增cur.close()                             #關閉遊標conn.commit()                           #提交資料(不提交是不能insert的語句不能出現)conn.close()                            #關閉串連

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/76/51/wKioL1ZO_nOASXr8AAAhZ7CugMM247.png" title="執行結果.png" width="132" height="100" border="0" hspace="0" vspace="0" style="width:132px;height:100px;" alt="wKioL1ZO_nOASXr8AAAhZ7CugMM247.png" />

4、刪除一條資料

MySQLdbconn = MySQLdb.connect(        =,        =,        =,        =,        =,        )cur = conn.cursor()cur.execute()                cur.close()conn.commit()conn.close()

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/76/52/wKiom1ZPAWTzAed6AAAZsvlngOU103.png" title="刪除.png" width="132" height="100" border="0" hspace="0" vspace="0" style="width:132px;height:100px;" alt="wKiom1ZPAWTzAed6AAAZsvlngOU103.png" />

5、修改一條資料

MySQLdbconn = MySQLdb.connect(        =,        =,        =,        =,        =,        )cur = conn.cursor()cur.execute()     cur.close()conn.commit()conn.close()

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/76/52/wKiom1ZPAtzy1pgwAABMB5Ktcm4814.png" title="改結果.png" width="132" height="100" border="0" hspace="0" vspace="0" style="width:132px;height:100px;" alt="wKiom1ZPAtzy1pgwAABMB5Ktcm4814.png" />

6、查詢資料

conn = .connect(        =,        =,        =,        =,        =,        )cur = conn.cursor()ll=cur.execute()ll

這樣只能查出有幾條資料,不能查出完整資料

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/76/52/wKiom1ZPBUjQ_g4dAAAUlpBZaXo365.png" title="條數.png" alt="wKiom1ZPBUjQ_g4dAAAUlpBZaXo365.png" />

調用fetchmang()函數後就可以利用for迴圈就可以查詢出資料

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/76/51/wKioL1ZPB02zk6u4AAB2lbT6ebI935.png" title="查詢資料庫.png" width="200" height="200" border="0" hspace="0" vspace="0" style="width:200px;height:200px;" alt="wKioL1ZPB02zk6u4AAB2lbT6ebI935.png" />













python 操作資料庫(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.