摘要:本地安裝配置安裝這個數(shù)據(jù)庫管理工具一會我們要手動創(chuàng)建數(shù)據(jù)庫數(shù)據(jù)表字段當(dāng)然也可以代碼創(chuàng)建增主機名這里是你的地址數(shù)據(jù)庫賬號數(shù)據(jù)庫密碼端口數(shù)據(jù)庫端口數(shù)據(jù)庫名基本語句初始化一個游標(biāo)對象數(shù)據(jù)庫操作語句執(zhí)行該語句關(guān)閉游標(biāo)對象關(guān)
本地安裝配置phpstduy
安裝這個數(shù)據(jù)庫管理工具 一會我們要手動創(chuàng)建數(shù)據(jù)庫 數(shù)據(jù)表 字段 當(dāng)然也可以代碼創(chuàng)建
1.增
import pymysql'''host 主機名 這里是你的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 主機名 這里是你的ip地址 #本地為localhostuser 數(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 主機名 這里是你的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 pymysqldb = 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()
'''host 主機名 這里是你的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 pymysqldb = 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 主機名 這里是你的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 pymysqldb = 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á)式
‘’’
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/124519.html
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊入門目標(biāo)使用搭建通過對數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計指南 RESTful API指南 依賴注入 D...
閱讀 769·2023-04-25 19:43
閱讀 4022·2021-11-30 14:52
閱讀 3855·2021-11-30 14:52
閱讀 3909·2021-11-29 11:00
閱讀 3838·2021-11-29 11:00
閱讀 3949·2021-11-29 11:00
閱讀 3613·2021-11-29 11:00
閱讀 6310·2021-11-29 11:00