python 實現(xiàn)mysql增刪查改示例代碼
本地安裝配置phpstduy
安裝這個數(shù)據(jù)庫管理工具 一會我們要手動創(chuàng)建數(shù)據(jù)庫 數(shù)據(jù)表 字段 當(dāng)然也可以代碼創(chuàng)建
1.增
import pymysql ''' host 主機(jī)名 這里是你的ip地址 user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標(biāo)對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標(biāo)對象 cursor.close()#關(guān)閉數(shù)據(jù)庫 rollback 回滾 ''' db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='text') sql = "insert into text(id,name) values (1,'老王')" #獲取下標(biāo) cursor = db.cursor() try: cursor.execute(sql) db.commit() print('插入成功') except: db.rollback() db.close()
2.刪
import pymysql ''' host 主機(jī)名 這里是你的ip地址 #本地為localhost user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標(biāo)對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標(biāo)對象 cursor.close()#關(guān)閉數(shù)據(jù)庫 rollback 回滾 ''' db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='text') sql ="delete from text where id=1 and name='老王' " #獲取下標(biāo) cursor = db.cursor() try: cursor.execute(sql) db.commit() print('刪除成功') except: db.rollback() db.close()
3.查
先添加2條數(shù)據(jù)因為刪除了
''' host 主機(jī)名 這里是你的ip地址 user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標(biāo)對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標(biāo)對象 cursor.close()#關(guān)閉數(shù)據(jù)庫 rollback 回滾 ''' import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='text') sql1 = "insert into text(id,name) values (1,'老李')" sql2 = "insert into text(id,name) values (2,'老王')" #獲取下標(biāo) cursor = db.cursor() try: cursor.execute(sql1) cursor.execute(sql2) db.commit() print('插入成功') except: db.rollback() db.close()
3.查
''' host 主機(jī)名 這里是你的ip地址 user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標(biāo)對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標(biāo)對象 cursor.close()#關(guān)閉數(shù)據(jù)庫 rollback 回滾 ''' import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='text') sql = "select id,name from text " #獲取下標(biāo) cursor = db.cursor() try: cursor.execute(sql) #查詢 result = cursor.fetchall() db.commit() print(f'查詢成功數(shù)據(jù)為:{result}') except: db.rollback() db.close()
4.改
''' host 主機(jī)名 這里是你的ip地址 user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標(biāo)對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標(biāo)對象 cursor.close()#關(guān)閉數(shù)據(jù)庫 rollback 回滾 ''' import pymysql db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='text') sql = "update text set name='小林' where id=1" #獲取下標(biāo) cursor = db.cursor() try: cursor.execute(sql) db.commit() print(f'修改成功') except: db.rollback() db.close()
總結(jié)
插入
INSERT INTO 表的名字(列名a,列名b,列名c) VALUES(值1,值2,值3);
刪
delete from 表名 where 條件表達(dá)式
查
select 列 from 表名
改
update 表名 set 要修改的值 where 條件表達(dá)式
以上就是python 實現(xiàn)mysql增刪查改示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python mysql增刪查改的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實現(xiàn)優(yōu)先級隊列結(jié)構(gòu)的方法詳解
優(yōu)先級隊列(priority queue)是0個或多個元素的集合,每個元素都有一個優(yōu)先權(quán),接下來就來看一下簡潔的Python實現(xiàn)優(yōu)先級隊列結(jié)構(gòu)的方法詳解:2016-06-06Python3.5內(nèi)置模塊之time與datetime模塊用法實例分析
這篇文章主要介紹了Python3.5內(nèi)置模塊之time與datetime模塊用法,結(jié)合實例形式分析了Python3.5 time與datetime模塊日期時間相關(guān)操作技巧,需要的朋友可以參考下2019-04-04python3中http協(xié)議提供文件服務(wù)器功能詳解
http協(xié)議是互聯(lián)網(wǎng)的通用基礎(chǔ)協(xié)議,也可以利用其來開發(fā)文件服務(wù)器,給客戶提供文件瀏覽,查看,下載,上傳等功能,這篇文章主要介紹了python3中http協(xié)議提供文件服務(wù)器功能,需要的朋友可以參考下2023-06-06python實現(xiàn)回旋矩陣方式(旋轉(zhuǎn)矩陣)
今天小編就為大家分享一篇python實現(xiàn)回旋矩陣方式(旋轉(zhuǎn)矩陣),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12