Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼
1.安裝pymysql
進(jìn)入cmd,輸入 pip install pymysql:
2.數(shù)據(jù)庫建表
在數(shù)據(jù)庫中,建立一個(gè)簡單的表,如圖:
3.簡單操作
3.1查詢操作
#coding=utf-8 #連接數(shù)據(jù)庫測(cè)試 import pymysql #打開數(shù)據(jù)庫 db = pymysql.connect(host="localhost",user="root",password="root",db="test") #使用cursor()方法獲取操作游標(biāo) cur = db.cursor() #查詢操作 sql = "select * from books" try: # 執(zhí)行sql語句 cur.execute(sql) results = cur.fetchall() #遍歷結(jié)果 for rows in results: id = rows[0] name = rows[1] price = rows[2] bookcount = rows[3] author = rows[4] print("id: {}, name: {}, price: {}, bookcount: {}, author: {}".format(id,name,price,bookcount,author)) except Exception as e: raise e finally: db.close()
運(yùn)行結(jié)果:
3.2插入操作
#coding=utf-8 #插入操作 import pymysql db = pymysql.connect(host="localhost",user="root",password="root",db="test") cur = db.cursor() sql = """insert into books(id,bookname,price,bookCount,author) values (4,'三體',20,3,'劉慈欣')""" try: cur.execute(sql) #提交 db.commit() except Exception as e: #錯(cuò)誤回滾 db.rollback() finally: db.close()
運(yùn)行結(jié)果:
3.3更新操作
#coding=utf-8 #更新操作 import pymysql db = pymysql.connect(host="localhost",user="root",password="root",db="test") # 使用cursor()方法獲取游標(biāo) cur = db.cursor() sql_update = "update books set bookname = '%s',author = '%s' where id = %d" try: cur.execute(sql_update % ("邊城","沈從文",4)) #提交 db.commit() except Exception as e: #錯(cuò)誤回滾 db.rollback() finally: db.close()
運(yùn)行結(jié)果:
3.4刪除操作
#coding=utf-8 #刪除操作 import pymysql db = pymysql.connect(host="localhost",user="root",password="root",db="test") #使用cursor()獲取操作游標(biāo) cur = db.cursor() sql_delete = "delete from books where id = %d" try: #向sql語句傳遞參數(shù) cur.execute(sql_delete % (1)) #提交 db.commit() except Exception as e: #錯(cuò)誤回滾 db.rollback() finally: db.close()
運(yùn)行結(jié)果:
到此這篇關(guān)于Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼的文章就介紹到這了,更多相關(guān)Python連接mysql數(shù)據(jù)庫及增刪改查操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中OpenCV實(shí)現(xiàn)查找輪廓的實(shí)例
本文將結(jié)合實(shí)例代碼,介紹 OpenCV 如何查找輪廓、獲取邊界框。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06利用Python Django實(shí)現(xiàn)簡單博客系統(tǒng)
這篇文章主要介紹了利用Python Django實(shí)現(xiàn)簡單博客系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05Python易忽視知識(shí)點(diǎn)小結(jié)
這篇文章主要介紹了Python易忽視知識(shí)點(diǎn),實(shí)例分析了Python中容易被忽視的常見操作技巧,需要的朋友可以參考下2015-05-05初步介紹Python中的pydoc模塊和distutils模塊
這篇文章主要介紹了Python中的pydoc模塊和distutils模塊,本文來自于IBM官方開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04使用Tensorflow實(shí)現(xiàn)可視化中間層和卷積層
今天小編就為大家分享一篇使用Tensorflow實(shí)現(xiàn)可視化中間層和卷積層,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01用python-webdriver實(shí)現(xiàn)自動(dòng)填表的示例代碼
這篇文章主要介紹了用python-webdriver實(shí)現(xiàn)自動(dòng)填表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python實(shí)現(xiàn)按關(guān)鍵字篩選日志文件
今天小編大家分享一篇python實(shí)現(xiàn)按關(guān)鍵字篩選日志文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python中opencv圖像疊加、圖像融合、按位操作的具體實(shí)現(xiàn)
opencv圖像操作可以更好更快的方便我們處理圖片,本文主要介紹了圖像疊加、圖像融合、按位操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07