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

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


1.增
import pymysql
'''
host 主機名 這里是你的ip地址
user 數(shù)據(jù)庫賬號
password 數(shù)據(jù)庫密碼
port 端口 mysql數(shù)據(jù)庫端口
db 數(shù)據(jù)庫名
基本語句
cursor = conn.cursor()#初始化一個游標對象
sql = "數(shù)據(jù)庫操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標對象
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,'老王')"
#獲取下標
cursor = db.cursor()
try:
cursor.execute(sql)
db.commit()
print('插入成功')
except:
db.rollback()
db.close()

2.刪
import pymysql
'''
host 主機名 這里是你的ip地址 #本地為localhost
user 數(shù)據(jù)庫賬號
password 數(shù)據(jù)庫密碼
port 端口 mysql數(shù)據(jù)庫端口
db 數(shù)據(jù)庫名
基本語句
cursor = conn.cursor()#初始化一個游標對象
sql = "數(shù)據(jù)庫操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標對象
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='老王' "
#獲取下標
cursor = db.cursor()
try:
cursor.execute(sql)
db.commit()
print('刪除成功')
except:
db.rollback()
db.close()

3.查
先添加2條數(shù)據(jù)因為刪除了
'''
host 主機名 這里是你的ip地址
user 數(shù)據(jù)庫賬號
password 數(shù)據(jù)庫密碼
port 端口 mysql數(shù)據(jù)庫端口
db 數(shù)據(jù)庫名
基本語句
cursor = conn.cursor()#初始化一個游標對象
sql = "數(shù)據(jù)庫操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標對象
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,'老王')"
#獲取下標
cursor = db.cursor()
try:
cursor.execute(sql1)
cursor.execute(sql2)
db.commit()
print('插入成功')
except:
db.rollback()
db.close()

3.查
'''
host 主機名 這里是你的ip地址
user 數(shù)據(jù)庫賬號
password 數(shù)據(jù)庫密碼
port 端口 mysql數(shù)據(jù)庫端口
db 數(shù)據(jù)庫名
基本語句
cursor = conn.cursor()#初始化一個游標對象
sql = "數(shù)據(jù)庫操作語句"
cursor.execute(sql)#執(zhí)行該語句
conn.commit()#關(guān)閉游標對象
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 "
#獲取下標
cursor = db.cursor()
try:
cursor.execute(sql)
#查詢
result = cursor.fetchall()
db.commit()
print(f'查詢成功數(shù)據(jù)為:{result}')
except:
db.rollback()
db.close()

4.改
''' host 主機名 這里是你的ip地址 user 數(shù)據(jù)庫賬號 password 數(shù)據(jù)庫密碼 port 端口 mysql數(shù)據(jù)庫端口 db 數(shù)據(jù)庫名 基本語句 cursor = conn.cursor()#初始化一個游標對象 sql = "數(shù)據(jù)庫操作語句" cursor.execute(sql)#執(zhí)行該語句 conn.commit()#關(guān)閉游標對象 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" #獲取下標 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 條件表達式
查
select 列 from 表名
改
update 表名 set 要修改的值 where 條件表達式
以上就是python 實現(xiàn)mysql增刪查改示例代碼的詳細內(nèi)容,更多關(guān)于python mysql增刪查改的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信