zoukankan      html  css  js  c++  java
  • Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理

     什么是mysql用户管理

        mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题,

            ATM购物车中的用户认证和mysql的用户认证原理是一样的,

            mysql中把文件称为表

            mysql自带的mysql数据库中有4个表用于管理的

            分别是:优先级从高到低  

            user --> db --> tables_priv --> columns_priv

                  1、创建用户的语句

                  2、授权的语句 *******

                  3grant [权限的名称 select insert ... | all] on 数据库.表名 to 用户名@主机地址 with grant option;

                  4、删除权限

                  5、删除用户

    1、创建用户的语句

        create user 用户名@"主机地址" identified by "密码"

        create user scote@"127.0.0.1" identified by "123"

        此处的主机地址不是服务器地址,而是表示这个账户可以在那台 电脑上登录

     

    2、授权的语句 *******

        语法:grant [权限的名称 select insert ... | all] on 数据库.表名 to 用户名@用户名@主机地址;

         # 授予scote这个用户所有数据库所有表中

         grant all on *.* to scote@"localhost"; 可以访问所有库和表

         grant all on day41.*to scote@"localhost"; 可以访问day41库的所有表

         grant all on day41.stu to scote@"localhost"; 可以访问day41库的stu

         grant select(id,name),insert(id,name) on day41.stu to scote@"localhost"; 仅能查看的添加day41库的stu表中的idname字段

         grant all on mydb.* to testDBA@"%" identified by "123";

    3grant [权限的名称 select insert ... | all] on 数据库.表名 to 用户名@主机地址 with grant option;

         with grant option 这个用户可以将他有的权限授予别的账户

         特点:如果授权时 用户不存在,直接自动创建用户

    4、删除权限

        revoke 权限的名称 on 数据库.表名 from用户名@"主机名";

        revoke all on *.* from scote@"localhost";

        update mysql.user set Grant_priv="N" where user = "scote" and host = "localhost";

       *.刷新权限表

          flush privileges;

    5、删除用户

        drop user 用户名@"主机地址";

    二:pymysql模块

    问:如何能在Python中去使用数据库存取数据?

    答:使用pymysql模块

      如何使用导入,pymysql使用步骤

           核心类Connect连接用和Cursor读写用

                1、与数据库服务器建立连接

                2、获取游标对象(用于发送和接收数据)

                3、用游标执行sql语句

                4、使用fetch方法来获取执行的结果

                5、关闭连接 先关游标 再关连接

      游标的常用方法

               1.创建游标  conn.cursor(指定查询结果的数据类型)

               2.excute 执行sql

               3.fetchone(sql只有一条记录时) many(sql有多条并且需要指定条数)  all(多余)

               4.scroll  用于修改游标的当前位置 

               注意:pymysql 默认不提交修改 但是注意(指的是对表中记录的操作不提交) 像删库,删表,是无法撤销的

    1 创建连接得到一个连接对象

            conn = pymysql.Connect(

                     host="127.0.0.1",  #数据库服务器主机地址

                     user="root",  #用户名

                     password="nuanixn",  #密码

                    database="day42",  #数据库名称

                    port=3306,  #端口号,这是一个整型,可选

                    charset="utf-8"  #编码,可选

                     )

       # 获取游标对象  pymysql.cursors.DictCursor指定,返回的结果类型为字典,默认是元组类型

           cursor  = conn.cursor(pymysql.cursors.DictCursor)

        # 查询数据

            #sql = "select *from;"  #分号可写可不写,不写的话会默认帮我们加上

             sql = "select *from emp"

        # 执行sql 如果是select,语句返回的是查询的条数

            res = cursor.execute(sql)

            print(res)

         # 获取查询的结果

            #print(cursor.fetchall())

             print(cursor.fetchone())

             print(cursor.fetchall()) # 像迭代,一个一个往下找

           #print(cursor.fetchmany(1))

             print(cursor.fetchall())

             #print(cursor.scroll(1,)) #找出文件中想要查询的某一个数据

        # scroll

            print(cursor.fetchone())

            cursor.scroll(-1)

            print(cursor.fetchall())

        # 关闭连接

            cursor.close()

            conn.close()

  • 相关阅读:
    290. Word Pattern
    283. Move Zeroes
    278. First Bad Version
    276. Paint Fence
    270. Closest Binary Search Tree Value
    [jobdu]二进制中1的个数
    [jobdu]丑数
    Skip List
    [wikioi]没有上司的舞会
    POJ3984 迷宫问题
  • 原文地址:https://www.cnblogs.com/zedong/p/9663129.html
Copyright © 2011-2022 走看看