• 多测师拱墅校区肖sir_高级金牌讲师_python+pymysq(1)


    python+pymysq

    一、python操作mysql数据库

    pymysql库名,关于数据库操作的库

    1、pymysql 下载

    (1)方法1:dos下

    pip  install   pymysql  

    (2)在pycharm中setting中

     (3)查看下pymsyql

     

     

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

    二、pymysql的使用

    (1)连接

    Connection连接方式

    (2)

    import pymysql #导入pymysql这个库
    db=pymysql.Connection(host="192.168.157.129",user="root",password="123456",
    database="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="select * from emp" #sql语句
    yb.execute(sql) #通过游标执行sql语句
    one=yb.fetchone()#通过游标获取第一行数据,就是显示第一行数据
    print(one)
    many=yb.fetchmany(size=2)#通过游标获取部分数据,size=显示的行数
    print(many)
    all=yb.fetchall() ##通过游标获取全部数据
    print(all)

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

    创建一个连接对象:

    db=pymysql.Connection(host="192.168.157.129",user="root",password="123456",
    database="dcs",port=3306 ,charset="utf8"

    )
    (1)Connection 连接
    (2)host主机
    (3)user用户
    (4)password 密码或者passwd

     (5)database 数据库或db  数据库

    (6)port  端口
    (7)charset="utf8" 字符编码格式


    ====================================
    通过for循环遍历所有数据
    import  pymysql #导入pymysql这个库
    # db=pymysql.Connection(host="192.168.157.129",user="root",password="123456",
    # database="dcs",port=3306 ,charset="utf8"
    #
    # )
    db=pymysql.connect(host="192.168.157.129",user="root",password="123456",
    db="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="select * from emp"
    yb.execute(sql)
    all=yb.fetchall()
    for i in all:
    print(i)

    ====================================
    python+pymysql增删改查
    (1)查:
    import  pymysql #导入pymysql这个库
    db=pymysql.connect(host="192.168.157.129",user="root",password="123456",
    db="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="select * from emp where dept2=101"#查询语句
    yb.execute(sql)
    all=yb.fetchall()
    for i in all:
    print(i)
    (2)删除:
    db=pymysql.connect(host="192.168.157.129",user="root",password="123456",
    db="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="DELETE from emp where name='xiaoniu' " #删除语句
    yb.execute(sql) #执行删除语句
    sql2="select * from emp where dept2=101" #查询语句
    yb.execute(sql2)
    all=yb.fetchall()
    for i in all:
    print(i)
    (3)改
    import  pymysql #导入pymysql这个库
    db=pymysql.connect(host="192.168.157.129",user="root",password="123456",
    db="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="UPDATE emp set sid=8888 where name='张二'" #修改sql语句
    yb.execute(sql)
    sql2="select * from emp where dept2=101"
    yb.execute(sql2)
    all=yb.fetchall()
    for i in all:
    print(i)
    (4)增
    import  pymysql #导入pymysql这个库
    db=pymysql.connect(host="192.168.157.129",user="root",password="123456",
    db="dcs",port=3306 ,charset="utf8"

    )
    yb=db.cursor() #创建游标对象
    sql="INSERT into emp(sid) VALUES(9999) "
    yb.execute(sql)
    sql2="select * from emp where sid=9999"
    yb.execute(sql2)
    all=yb.fetchall()
    for i in all:
    print(i)
    =======================================================
    pymysql封装:
    import  pymysql
    class Db():
    def __init__(self,host,user,passwd,db,port):
    self.host=host
    self.user=user
    self.passwd=passwd
    self.db=db
    self.port=port
    def lj(self):
    ljmysql=pymysql.connect(host=self.host,user=self.user,password=self.passwd,
    db=self.db,port=self.port,charset="utf8")
    return ljmysql
    def one(self,sql):
    dx=self.lj()
    yb=dx.cursor()
    yb.execute(sql)
    one1=yb.fetchone()
    print(one1)

    def many(self, sql,s):
    dx = self.lj()
    yb = dx.cursor()
    yb.execute(sql)
    many2 = yb.fetchmany(size=s)
    print(many2)

    def all(self, sql):
    dx = self.lj()
    yb = dx.cursor()
    yb.execute(sql)
    all1 = yb.fetchall()
    print(all1)
    if __name__ == '__main__':
    d=Db(host="192.168.157.129",user="root",passwd="123456",
    db="dcs",port=3306 )
    # d.one("select * from emp where dept2=101")
    d.many("select * from emp ",5)
    # d.all("select * from emp ")


  • 相关阅读:
    Python之路【第四十五篇】:django日更
    Python之路【第四十四篇】:django日更
    C++ 调用动态链接库
    博客园美化
    postgresql中的UUID
    使用rustup安装rust环境
    MySQL 查询做排名
    Docker
    CentOS安装Docker和基础操作
    切换CentOS7的yum源为阿里源
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/16389927.html
Copyright © 2020-2023  润新知