標籤: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)