#python + Mysqldb
#Mpymysql 库名,关于数据库操作的库
#先进行安装这个库
#方法一:pip install pymysql
#方法二:在pycharm 中搜素pymysql
案例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import pymysql #或者引用pymysql db = pymysql.connect( "192.168.153.131" , "root" , "123456" , "baoan" , 3306 ,charset = 'utf8' ) #连接数据库 cursor = db.cursor() #游标对象 sql = "select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" # #cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) cursor.execute(sql) #查询更新后的数据 one = cursor.fetchone() #游标获取并占用一条数据 print (one) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import pymysql #或者引用pymysql db = pymysql.connect( "192.168.153.131" , "root" , "123456" , "baoan" , 3306 ,charset = 'utf8' ) #连接数据库 cursor = db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 sql0 = "select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" # #cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) # cursor.execute(sql) #查询更新后的数据 # one=cursor.fetchone() #游标获取并占用一条数据 # print (one) # cursor.execute(sql4) #删除符合条件的数据 # cursor.execute(sql) #查询符合条件的数据 # two=cursor.fetchone() #获取占用这条数据 # print (two) cursor.execute(sql0) #执行语句 all = cursor.fetchall() #游标获取未被占用的所有数据. print ( all ) |
插入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import pymysql #或者引用pymysql db = pymysql.connect( "192.168.153.131" , "root" , "123456" , "baoan" , 3306 ,charset = 'utf8' ) #连接数据库 cursor = db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 sql2 = "insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" # sql4="delete from dept where dept1=106" cursor.execute(sql2) #执行语句 # #cursor.execute(sql3) # #cursor.execute(sql4) # cursor.execute(sql1) #查询更新后的数据 one = cursor.fetchone() #游标获取并占用一条数据 print (one) |
修改数据
import pymysql #或者引用pymysql
db=pymysql.connect("192.168.153.131","root","123456","baoan",3306,charset='utf8') #连接数据库
cursor=db.cursor() #游标对象
#sql="select * f rom dept where dept1=102" #sql语句
#sql0="select * from dept" #sql语句
#sql1="select count(*) from dept" #sql语句
#sql2="insert into dept(dept1,dept_name) values(106,'ittest') "
sql3="update dept set dept_name = 'it' where dept1=106"
# sql4="delete from dept where dept1=106"
#cursor.execute(sql2) #执行语句
cursor.execute(sql3)
#cursor.execute(sql4)
# cursor.execute(sql1) #查询更新后的数据
one=cursor.fetchone() #游标获取并占用一条数据
print (one
删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import pymysql #或者引用pymysql db = pymysql.connect( "192.168.153.131" , "root" , "123456" , "baoan" , 3306 ,charset = 'utf8' ) #连接数据库 cursor = db.cursor() #游标对象 #sql="select * f rom dept where dept1=102" #sql语句 #sql0="select * from dept" #sql语句 #sql1="select count(*) from dept" #sql语句 #sql2="insert into dept(dept1,dept_name) values(106,'ittest') " #sql3="update dept set dept_name = 'it' where dept1=106" sql4 = "delete from dept where dept1=106" #cursor.execute(sql2) #执行语句 #cursor.execute(sql3) cursor.execute(sql4) # cursor.execute(sql1) #查询更新后的数据 one = cursor.fetchone() #游标获取并占用一条数据 print (one) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架