MySQL数据库(五)—— 用户管理、pymysql模块

用户权限管理、pymysql模块

一、用户管理(权限管理)

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

# 优先级从高到低
user > db > tables_priv > columns_priv
user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段
db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段
tables_priv #该表放行的权限。针对:某一张表,以及该表下的所有字段
columns_priv #该表放行的权限,针对:某一个字段
# 1.创建用户      主机号是客户端的主机地址,% 代表任意一条主机
create user 用户名@"主机地址" identified by '密码';
eg:create user tom@
'localhost' identified by '123';
# 2.授权 []中为可选内容,with grant option 是给用户添加授权权限,all 代表所有除了grant以外的权限。若是用户名不存,则会先创建该用户名,再授权。 grant [权限名字:select、insert、update……、all] on 数据库名.表名 to 用户名@"主机地址" [with grant option];
      # 权限可以是授予数据库、表、某些字段的权限
eg: grant all on mydb.
* to tom@'127.0.0.1' grant all on mydb.teacher to tom@'127.0.0.1' with grant option
# 3.解除授权 revoke [权限名字:select、insert、delete……all] on 数据库名.表名 from 用户名@"主机地址"
eg:revoke select on mydb.
* from tom@'127.0.0.1';
# 4.删除用户 drop user 用户名@"主机地址"

二、pymysql模块

pymysql模块用于Python程序与MySQL的连接

#安装
pip3 install pymysql

1、查询

# 1.连接数据库,创建连接,获得一个连接对象
conn = pymysql.Connect(
    host='127.0.0.1',  # 主机地址
    user='root',  # 用户名
    password='hf19970124',  # 密码
    database='mydb',  # 数据库名称
    port=3306,  # 端口号  可选
    charset='utf8'  # 编码   可选
)

# 2.获取游标对象,游标封装的读和写的操作
cursor = conn.cursor(pymysql.cursors.DictCursor)  # pymysql.cursors.DictCursor指定返回的结果为字典,不写默认为元组

# 3.查询语句
sql = "select * from teacher where name-'%s' and password = '%s'%('tom','123')"   # 注意%s需要加引号,并且单引号、双引号的嵌套使用

# 4.执行sql语句,如果是查询,返回查询的条数
res = cursor.execute(sql)
print(res)

# 5.获取查询的结果
print(cursor.fetchall())
# print(cursor.fetchone())


# 6.关闭连接
cursor.close()
conn.close()
2、fetchall、fetchone、fetchmany与scroll
fetchall、fetchone、fetchmany 用于获取查询结果
scroll 用于设置游标的位置
cursor.fetchall()   # 获取所有的结果
cursor.fetchone()   #获取结果中的第一条数据,取出一天,游标的位置就往下移一个,下一次取从新位置开始
cursor.fetchmany(size)   # 获取size条数据


# scroll  可用于设置游标的位置
cursor.scroll(1,mode='relative')    # 相对位置,游标位置从当前位置后移一个,下一次取结果,从第二条数据开始
cursor.scroll(-1)     # 默认为相对位置,游标位置从当前位置退一个
cursor.scroll(3,mode='absolute') # 绝对位置,游标位置从头开始移动3个
3、增、删、改数据
(1)在增删改数据的时候,执行完SQL语句,必须提交  conn.commint()
# 在增删改数据的时候,执行完SQL语句,必须提交,否则在数据库中的数据不会改变
import pymysql

conn = pymysql.Connect(
    host='127.0.0.1',
    user='root',
    password='hf19970124',
    database='mydb',
    port=3306,
    charset='utf8'
)

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

sql = 'insert into stu values(1,"ton"),(2 ,"jack")'
# sql = 'update stu set name="TOM" where id = 2'
# sql = 'delete from stu where name = "tom"'
 
cursor.execute(sql)

conn.commit()  # 提交

cursor.close()
conn.close()
提交 conn.commit()

(2)修改数据时,如果发生错误,数据应该回到起始值,如果修改失败,就会抛出异常,可以对异常进行处理,只要抛出异常就将数据撤销  conn.rollback()

import pymysql
# 创建链接得到一个链接对象
conn = pymysql.Connect(
    host="127.0.0.1",    # 数据库服务器主机地址
    user="root",  # 用户名
    password="admin", # 密码
    database="day42", #数据库名称
    port=3306, # 端口号 可选 整型
    charset="utf8" # 编码  可选
)
# 获取游标对象  pymysql.cursors.DictCursor指定 返回的结果类型 为字典  默认是元祖类型
cursor = conn.cursor(pymysql.cursors.DictCursor)

try:
    cursor.execute("update moneyTable set money = money - 50 where name = '小明'")
    #如果小花的账户出问题了 无法更新数据 那就需要回滚
    cursor.execute("update moneyTable set money = money + 50 where name = '小花'")
    conn.commit()
except:
    conn.rollback()



cursor.close()
conn.close()
撤销 conn.roback()
原文地址:https://www.cnblogs.com/linagcheng/p/9662824.html