python之MySQL學習——資料操作

來源:互聯網
上載者:User

標籤:sel   資料庫   exe   insert   串連   word   bsp   use   ase   

1.增

 1 import pymysql as ps 2  3 # 開啟資料庫連接 4 db = ps.connect(host=‘localhost‘, user=‘root‘, password=‘123456‘, database=‘test‘, charset=‘utf8‘) 5  6 # 建立一個遊標對象 7 cur = db.cursor() 8  9 sql = "INSERT INTO USER(NAME,SEX) VALUES(%s,$s)"10 11 cur.execute(sql, (‘小美‘,‘女‘))12 13 print("OK")14 15 # 提交事務(這個一定要主動提交,不然在資料庫中操作增、刪結果不改變)16 db.commit()17 18 # 關閉遊標19 cur.close()20 21 # 關閉資料庫22 db.close()

2.刪除

 1 import pymysql as ps 2  3 # 開啟資料庫連接 4 db = ps.connect(host=‘localhost‘, user=‘root‘, password=‘123456‘, database=‘test‘, charset=‘utf8‘) 5  6 # 建立一個遊標對象 7 cur = db.cursor() 8  9 cur.execute(‘DELETE FROM USER WHERE NAME="小芳"‘)10 11 a = cur.execute(‘SELECT * FROM  USER‘)    # 傳回值是一個int類型的值12 13 # 提交14 db.commit()15 16 # 查看記錄數17 print(a)18 19 # 關閉遊標20 cur.close()21 22 # 關閉資料庫23 db.close()

 

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.