python 實(shí)現(xiàn)mysql增刪查改示例代碼
本地安裝配置phpstduy

安裝這個(gè)數(shù)據(jù)庫(kù)管理工具 一會(huì)我們要手動(dòng)創(chuàng)建數(shù)據(jù)庫(kù) 數(shù)據(jù)表 字段 當(dāng)然也可以代碼創(chuàng)建


1.增
import pymysql
'''
host 主機(jī)名 這里是你的ip地址
user 數(shù)據(jù)庫(kù)賬號(hào)
password 數(shù)據(jù)庫(kù)密碼
port 端口 mysql數(shù)據(jù)庫(kù)端口
db 數(shù)據(jù)庫(kù)名
基本語句
cursor = conn.cursor()#初始化一個(gè)游標(biāo)對(duì)象
sql = "數(shù)據(jù)庫(kù)操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標(biāo)對(duì)象
cursor.close()#關(guān)閉數(shù)據(jù)庫(kù)
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ù)庫(kù)賬號(hào)
password 數(shù)據(jù)庫(kù)密碼
port 端口 mysql數(shù)據(jù)庫(kù)端口
db 數(shù)據(jù)庫(kù)名
基本語句
cursor = conn.cursor()#初始化一個(gè)游標(biāo)對(duì)象
sql = "數(shù)據(jù)庫(kù)操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標(biāo)對(duì)象
cursor.close()#關(guān)閉數(shù)據(jù)庫(kù)
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ù)因?yàn)閯h除了
'''
host 主機(jī)名 這里是你的ip地址
user 數(shù)據(jù)庫(kù)賬號(hào)
password 數(shù)據(jù)庫(kù)密碼
port 端口 mysql數(shù)據(jù)庫(kù)端口
db 數(shù)據(jù)庫(kù)名
基本語句
cursor = conn.cursor()#初始化一個(gè)游標(biāo)對(duì)象
sql = "數(shù)據(jù)庫(kù)操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標(biāo)對(duì)象
cursor.close()#關(guān)閉數(shù)據(jù)庫(kù)
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ù)庫(kù)賬號(hào)
password 數(shù)據(jù)庫(kù)密碼
port 端口 mysql數(shù)據(jù)庫(kù)端口
db 數(shù)據(jù)庫(kù)名
基本語句
cursor = conn.cursor()#初始化一個(gè)游標(biāo)對(duì)象
sql = "數(shù)據(jù)庫(kù)操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標(biāo)對(duì)象
cursor.close()#關(guān)閉數(shù)據(jù)庫(kù)
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ù)庫(kù)賬號(hào)
password 數(shù)據(jù)庫(kù)密碼
port 端口 mysql數(shù)據(jù)庫(kù)端口
db 數(shù)據(jù)庫(kù)名
基本語句
cursor = conn.cursor()#初始化一個(gè)游標(biāo)對(duì)象
sql = "數(shù)據(jù)庫(kù)操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標(biāo)對(duì)象
cursor.close()#關(guān)閉數(shù)據(jù)庫(kù)
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 實(shí)現(xiàn)mysql增刪查改示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python mysql增刪查改的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python爬蟲控制aiohttp并發(fā)數(shù)量方式
這篇文章主要介紹了python爬蟲控制aiohttp并發(fā)數(shù)量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python Web框架之Django框架cookie和session用法分析
這篇文章主要介紹了Python Web框架之Django框架cookie和session用法,結(jié)合實(shí)例形式分析了Django框架cookie和session的常見使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié)
這篇文章主要介紹了關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié),Series序列,是一種一維的結(jié)構(gòu),類似于一維列表和ndarray中的一維數(shù)組,但是功能比他們要更為強(qiáng)大,Series由兩部分組成:索引index和數(shù)值values,本篇對(duì)其用法做出總結(jié)2023-07-07
Python中導(dǎo)入自定義模塊的幾種方法總結(jié)
這篇文章主要介紹了Python中導(dǎo)入自定義模塊的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
python 實(shí)現(xiàn)的IP 存活掃描腳本
這篇文章主要介紹了python 實(shí)現(xiàn)的IP 存活掃描腳本,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
詳解Python中Pandas read_csv參數(shù)使用
在使用 Pandas 進(jìn)行數(shù)據(jù)分析和處理時(shí),read_csv 是一個(gè)非常常用的函數(shù),本文將詳細(xì)介紹 read_csv 函數(shù)的各個(gè)參數(shù)及其用法,希望對(duì)大家有所幫助2022-10-10

