用户权限,pymysql

单表查询的完整语法

select [distinct] [*|字段|聚合函数|表达式] from table
where
group by
having
distinct
order by
limit

mysql 用户管理

权限优先级:
user >db>table_priv>columns_priv


创建账号:
create user ⽤户名@"ip地址" "identified" by 密码;
create user tom@"192.168.101" identified by "123";
该语句表⾯tom只能在101机器上使⽤,别的机器就⽆法登录
⽤%可以表示在任意机器可⽤
注意:该⽅式创建的账号没有任何权限
需要使⽤授权语句

授权:
授权语句执行时如果账号不存在会自动创建账号,默认只有root才能为其他账号授权


grant all on *.* to tom@'localhost' identified by '123';

该语句中的all不包括grant 权限,要获得授权权限需要在后面加上with grant optin


*.*表示可以操作任何数据库,任何表

db.*表示可以操作db数据库下的任何表

db.t1表示可操作db数据库下的t1表


grant select(id) on db.t1 to tom@'localhost' identified by '123'

表示只能查看db库下t1表的id


删除用户 drop user@'host'

刷新权限表 flush privileges


pynysql

获取链接对象
conn=pymysql.connect(
host='127.0.0.1',
user=root,
password='123';
database='day0916';
port=3306,
charset='utf8')



获取游标对象
cursor=conn.cursor(pymysql.cursor.DictCursor)
#其中pymysql.cursor.DictCursor是指定返回的结果类型为字典,不写默认就是元组类型


查询数据

res=cursor.execute('select * from emp')#如果是select语句返回的res则是查询结果的条数


获取执行结果
提取数据
fetchone()获取一条数据
fetchmany(2)获取指定条数的数据
fetchall()获取剩余全部数据

print(cursor.fetchone())#all,many

scroll
cursor.scroll(1,'absolute')#absolute绝对移动
cursor.scroll(1,'relative')#absolute相对移动


commit与rollback
pymsql默认是不对表中记录的修改操作提交的,但是删库删表是不可恢复的

cursor.execute('insert into emp...')
conn.commit()#提交


try:
cursor.execute('update a...')
cursor.execute('update b...')
conn.commit()
except:
conn.rollback()
要a与b的更新操作都成功才提交修改操作,否则回滚,保证了数据的安全性

原文地址:https://www.cnblogs.com/fushaunglin/p/9665482.html