3.pymysql的应用

使用pip安装PyMysql模块

  • 安装依赖包
    • yum install -y gcc
  • 本地安装
    • pip3 install PyMySQL-0.8.0.tar.gz
  • 在线安装(建议使用国内镜像站点)
    • mkdir ~/.pip
    • vim ~/.pip/pip.conf
      [global]
      index-url = http://mirrors.163.com/pypi/simple
      [install]
      trusted-host = mirrors.163.com
    • 在虚拟环境中安装pymysql
      • 创建虚拟环境
        python3 -m venv ~/mypy
      • 激活虚拟环境
        source ~~/mypy/bin/activate
        pip3 install pymysql
        或 pip3 install pymysql==0.9.2 #安装指定版本的pymysql

pymysql的应用

  • 实现效果
    • 连接数据库,实现CRUD
  • 准备数据库
    • 员工信息表:工号,姓名,出生日期,电话,email,部门ID
    • 部门表:部门ID,部门名
    • 工资表:ID号,工资日,工号,基本工资,绩效
    • 在数据库服务器增加授权用户,如:
    grant all on test.* to root@'%' identified by 'a'
    
  • 创建到数据库服务器的连接
    import pymysql
    conn = pymysql.connect(
    host = '192.168.1.10',
    port = 3306,
    user = 'root',
    passwd = 'a',
    db = 'test',
    charset = 'utf8'
    )
    
  • 创建游标.可以理解游标就是打开文件时返回的文件对象,用过游标可以执行sql语句,对数据库进行CRUD(增删改查)
    cursor = conn.cursor()
    
  • 编写sql语句(创建表)
    create_dep = '''CREATE TABLE departments(
    dep_id INT,        #部门ID
    dep_name VARCHAR(20),  #部门名
    PRIMARY KEY(dep_id)    #根据1NF,每个表中都要有主键
    )'''
    create_emp = '''CREATE TABLE employees(
    emp_id INT,
    emp_name VARCHAR(20),
    birth_date DATE,
    phone VARCHAR(11),
    email VARCHAR(50),
    dep_id INT,
    PRIMARY KEY(emp_id),
    FOREIGN KEY(dep_id) REFERENCES   departments(dep_id)     #创建外键
    )'''
    create_sal = '''CREATE TABLE salary(
    id INT,
    date DATE,
    emp_id INT,
    basic INT,
    awards INT,
    PRIMARY KEY(id),
    FOREIGN KEY(emp_id) REFERENCES employees(emp_id)
    )'''
    
  • 执行sql语句
    cursor.execute(create_dep)
    cursor.execute(create_emp)
    cursor.execute(create_sal)
    
  • 确认
    conn.commit()
    
  • 关闭游标和连接
    cursor.close()
    conn.close()
    

编写CRUD语句

  • 向部门表中插入数据
    import pymysql
    conn = pymysql.connect(    #连接到数据库
      host = '192.168.1.10',
      port = 3306,
      user = 'root',
      passwd = 'b',
      db = 'ceshi',
      charset = 'utf8'
    )
    cursor = conn.cursor()    #创建游标
    insert_dep = 'insert into departments VALUES (%s, %s)'  #先用%s,%s占位
    hr = (1, '人事部')
    ops = (2, '运维部')
    dev = (3, '开发部')
    qa = (4, '测试部')
    market = (5, '市场部')
    finance = (6, '财务部')
    salas = (7, '销售部')
    deps = [hr, ops, dev, qa, market, finance, sales]
    cursor.executemany(insert_dep, deps) #一次插入多次数据
    conn.commit()    #增删改都需要确认
    cursor.close()    #程序运行完成,要断开连接
    conn.close()
    
  • 删除销售部
    del_dep = 'delete from departments where dep_name=%s'
    cursor.execute(del_dep, ('销售部',))  #execute()接收的参数是元组,单元素元组后要加 逗号
    
  • 修改
    update_dep = 'update departments set dep_name = %s where dep_name = %s'
    cursor.execute(update_dep, ('人力资源部', '人事部'))
    
  • 查询(不需要commit确认)
    #查询
    query_dep = 'select * from departments'
    #执行sql语句
    cursor.execute(query_dep)
    #取数据
    result = cursor.fetchone()
    print(result)
    
  • 使用游标来查询
    query_dep = 'select * from departments'
    cursor.execute(query_dep)
    cursor.scroll(3, mode = 'absolute')  #必须从头开始数,进行移动
    cursor.scroll(1)  #默认以相对位置移动,即相对当前位置移动
    

以上代码,如有出错,请严厉指出,不用客气,谢谢

你可能感兴趣的:(3.pymysql的应用)