当前位置: 首页 > news >正文

python+pymysql

python操作mysql

 一、python操作数据库

1、下载pymysql 库,

方法一:pip3  install   pymysql  或pip   install  pymysql

 

方法二:在pycharm中setting下载pymysql

===============================

2、打开虚拟机上的数据库

===============================

3、pymysql连接

db=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')

(1)连接方式:pymysql.Connection 或者pymysql.connect 

(2)包含内容
a.host 主机:填写IP地址
b.user  数据库用户名
c.password 或passwd  密码:
d.databases  或db  库名
e.port  端口  :默认3306
f.chardet ='utf8'    编码格式
(2)将连接内容设置成一个变量,然后创建一个游标对象
db.cursor
(3)使用游标对象去执行sql语句
(4)在根据需要显示内容使用 fetchone,fetchall,fetchmany

案例1:查询

import  pymysql
db=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
yb=db.cursor()#创建游标
sql="select *  from  emp"
yb.execute(sql)
# one=yb.fetchone() #获取第一行数据
# print(one)
# many=yb.fetchmany(size=3)
# print(many)#获取部分数据
all=yb.fetchall() #获取所有数据
print(all)案例2:删除语句
import  pymysql
db=pymysql.connect(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
#db1=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
yb=db.cursor()#创建游标
sql="delete  from  emp where name='张三'"
yb.execute(sql)案例3:
插入数据
import  pymysql
db=pymysql.connect(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
#db1=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
yb=db.cursor()#创建游标
sql="insert into emp VALUES ('1879','张三',55,'1971/10/20',7300,'101');"
yb.execute(sql)
sql1="select  *  from  emp"
yb.execute(sql1)
all=yb.fetchall() #获取所有数据
for i  in  all:print(i)
案例3:
修改数据
import  pymysql
db=pymysql.connect(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
#db1=pymysql.Connection(host="192.168.157.128",user="root",password="123456",database="test",port=3306,charset='utf8')
yb=db.cursor()#创建游标
sql="UPDATE  emp  SET name='zhan'  where  sid=1674 "
yb.execute(sql)
sql1="select  *  from  emp"
yb.execute(sql1)
all=yb.fetchall() #获取所有数据
for i  in  all:print(i)
============================================
import  pymysql
class   Db_hz(object):def  __init__(self,host,user,passwd,db,port):self.host=hostself.user=userself.passwd=passwdself.db=dbself.port=portdef lj(self):l=pymysql.Connection(host=self.host,user=self.user,passwd=self.passwd,db=self.db,port=self.port,charset="utf8")return ldef one(self,sql):d=self.lj()yb=d.cursor()yb.execute(sql)one1=yb.fetchone()print(one1)def many(self, sql):d = self.lj()yb = d.cursor()yb.execute(sql)many= yb.fetchmany(size=2)print(many)def all(self, sql):d = self.lj()yb = d.cursor()yb.execute(sql)all = yb.fetchall()print(all)
if __name__ == '__main__':dx=Db_hz("192.168.157.128","root","123456","test",3306)# dx.one("select * from  emp")# dx.many("select * from  emp")dx.all("select * from  emp")
http://www.lryc.cn/news/520198.html

相关文章:

  • 基于 Selenium 实现上海大学校园网自动登录
  • 啥!GitHub Copilot也免费使用了
  • Spring配置文件中:密码明文改为密文处理方式(通用方法)
  • Linux下ext2文件系统
  • BUUCTF:web刷题记录(1)
  • 【微服务】面试题 6、分布式事务
  • 【2024年华为OD机试】(C卷,100分)- 分割均衡字符串 (Java JS PythonC/C++)
  • Spring Data Elasticsearch简介
  • GESP202312 四级【小杨的字典】题解(AC)
  • 键盘过滤驱动
  • dolphinscheduler2.0.9升级3.1.9版本问题记录
  • 【权限管理】Apache Shiro学习教程
  • 9.4 visualStudio 2022 配置 cuda 和 torch (c++)
  • python特殊参数
  • Ubuntu系统Qt的下载、安装及入门使用,图文详细,内容全面
  • elasticsearch集群部署
  • 初学stm32 --- DAC模数转换器工作原理
  • 保证Mysql数据库到ES的数据一致性的解决方案
  • Flutter Xcode 16+ iOS 18.1 使用image_pickers无法弹出选择图片的视图问题
  • socket网络编程-TC/IP方式
  • 《分布式光纤测温:解锁楼宇安全的 “高精度密码”》
  • C语言基本知识复习浓缩版:数组
  • Python贪心
  • rk3568 内核态OOM内存泄漏kmemleak使用
  • ASP.NET Core - 日志记录系统(二)
  • 阿里云直播互动Web
  • 解锁无证身份核验:开启便捷安全新征程
  • [DO374] Ansible 配置文件
  • 【杂谈】-50+个生成式人工智能面试问题(四)
  • RuoYi Cloud项目解读【四、项目配置与启动】